c에서 __THROW의 사용방법

cho's의 이미지

/usr/include에 있는 헤더 파일을 보고 저도 함 그렇게 해 봤는데 도체가 어케 코드적용을 하고 어케 작동을 하는지 모르겠네요.
간단히 제가 만든 샘플을 적어보겠습니다.

#ifndef __STR_UTILS_H__
#define __STR_UTILS_H__

#include <features.h>

__BEGIN_DECLS
#define __need_size_t
#define __need_NULL
#include <stddef.h>

extern char * strswap( char *in, char what, char whereof) __THROW;
extern char * strlower( char * in ) __THROW;
extern char * strdelspacesbeforecommand( char * in ) __THROW;
extern char * strdelspacesbeforecommand( char * in ) __THROW;
extern char * strdelspaces( char * in ) __THROW;

__END_DECLS


#endif

이렇게 /usr/include에 있는것들을 보고 함 해봤거든요. 그 담에 c파일에서는 어케 해야 하는지 잘 모르겠네요.
char * strlower( char * in )
{
	int i;
	for( i=0;i<(int)strlen( in );i++ ) in[i]=( in[i] >= 'A' ? ( in[i] <= 'Z' ?  in[i]+='A' : in[i] ) : in[i] );
	return in;
}

char * strdelspacesbeforecommand( char * in )
{
 int    c = 0;
 char * out;
 if ( strlen( in ) == 0 ) return NULL;
 while ( in[c] == ' ' ) c++;
 if ( c != 0 )
  {
   out=malloc( strlen( in ) - c  + 1 );
   memcpy( out,&in[c],strlen( in ) - c + 1 );
  }
  else out=in;
 return out;
}

char * strswap( char * in,char what,char whereof )
{
	int    i;
	if ( strlen( in ) == 0 ) return NULL;
	for ( i=0;i<(int)strlen( in );i++ )
		if ( in[i] == what ) in[i]=whereof;
	return in;
}

다음과 같습니다. exception을 C에서도 던질수 있도록 하는것 같은데 어케 하는건지 잘 모르겠네요.분명 _THROW는 먼가를 던지는건데 이 매크로가 어떤건지도 잘 모르겠구요. 그냥 따라해보고 main으로 만들어봤는데 __THROW없는거랑 아무차이가 없네요.. 어케해야 되나요? 고수님들의 조언부탁드립니다.

참 c로 컴파일 하면 이상이 없는데 g++로 cpp로 컴파일 해보면 declaration of 'char* strlower(char*)' throws diffferent exceptions라고 나오네요....
지금 c랑 cpp의 class랑 섞어서 쓰고 있는데 이게 문제인가요?

primee의 이미지

__THROW 는 아마 C에서 사용돼는것이 아니라 C++호환때문에
존재하는것으로 압니다.

10년후의 나와 인사하자!!

pynoos의 이미지

headformat wrote:
__THROW 는 아마 C에서 사용되는 것이 아니라 C++호환때문에
존재하는 것으로 압니다.

맞습니다.

vim을 사용하신다면 (Linux 면 당연 vim이겠지요.)

/usr/include/stdio.h 에서

__THROW 에 커서를 옮기시고

ctrl+W, ctrl+I

를 차례로 누르시거나.. 매크로임이 확실하므로

ctrl+W, ctrl + D

를 누르시면 원하는 선언을 보실 수 있을 것입니다.

litdream의 이미지

방금 __THROW 가 무엇인지 궁금해서 찾던중 이글을 읽게 되었습니다.
그런데, 고수님들의 깊은 심중이 정말 궁금해지네요.

sys/cdef.h 에 보면,
#define __THROW

만 해놨으니깐, 없는것과 같은것은 이해가 되었습니다만,
이것이 C++ 와 호환이 되도록 한다는것에서 이해력의 한계를 느낍니다.

혹시, 다음것과도 관계가 있나요? 있다면, 어떻게...?


#ifdef  __cplusplus
extern "C" {
#endif

.... 정의문이 들어가고 ....

#ifdef  __cplusplus
}
#endif

삽질의 대마왕...

sunyzero의 이미지

extern "C" 는 C++에서 해당 함수의 호환성을 가져오기 위해서 선언된 구문입니다.

아 참, 이것을 name mangling 에서의 문제를 피하기 위해서 만들어진 것이라고 하죠. 함수가 실제로 링킹되는 과정에서 실제 이름과 header에서의 이름과 서로 일치하도록 해준답니다.

========================================
* The truth will set you free.

pynoos의 이미지

C++에서는 함수 뒷부분에 throw를 넣어서 어떤 type 의 throw가 일어나는지를 명시해줄 수가 있습니다. 또한 없다면 없다고 명시해줄 수도 있습니다.

C 함수 프로토 타입은 throw해줄일이 없으므로 없다고 표시해주면 컴파일러가 그에 따른 최적화를 할 수 있기 때문에 넣어 주는 것입니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.