|
需要特别注意的是,如果myprint是一个函数的成员函数,那么m和n的值可有点“悬乎”了,例如: //m=3;n=4 extern void myprint(int l,const char *format,...) __attribute__((format(printf,3,4))); 其原因是,类成员函数的第一个参数实际上一个“隐身”的“this”指针。(有点C++基础的都知道点this指针,不知道你在这里还知道吗?) 这里给出测试用例:attribute.c,代码如下: 1: 2:extern void myprint(const char *format,...) __attribute__((format(printf,1,2))); 3: 4:void test() 5:{ 6: myprint("i=%d\n",6); 7: myprint("i=%s\n",6); 8: myprint("i=%s\n","abc"); 9: myprint("%s,%d,%d\n",1,2); 10:}
运行$gcc –Wall –c attribute.c attribute后,输出结果为:
attribute.c: In function `test': attribute.c:7: warning: format argument is not a pointer (arg 2) attribute.c:9: warning: format argument is not a pointer (arg 2) attribute.c:9: warning: too few arguments for format
如果在attribute.c中的函数声明去掉__attribute__((format(printf,1,2))),再重新编译,既运行$gcc –Wall –c attribute.c attribute后,则并不会输出任何警告信息。 注意,默认情况下,编译器是能识别类似printf的“标准”库函数。 __attribute__ noreturn 该属性通知编译器函数从不返回值,当遇到类似函数需要返回值而却不可能运行到返回值处就已经退出来的情况,该属性可以避免出现错误信息。C库函数中的abort()和exit()的声明格式就采用了这种格式,如下所示:
extern void exit(int) __attribute__((noreturn)); extern void abort(void) __attribute__((noreturn));
为了方便理解,大家可以参考如下的例子:
//name: noreturn.c ;测试__attribute__((noreturn)) extern void myexit();
int test(int n) { if ( n > 0 ) { myexit(); /* 程序不可能到达这里*/ } else return 0; }
编译显示的输出信息为:
$gcc –Wall –c noreturn.c noreturn.c: In function `test': noreturn.c:12: warning: control reaches end of non-void function
警告信息也很好理解,因为你定义了一个有返回值的函数test却有可能没有返回值,程序当然不知道怎么办了!
|