__attribute__((noreturn))的用法

这个属性告诉编译器函数不会返回,这可以用来抑制关于未达到代码路径的错误。 C库函数abort()和exit()都使用此属性声明:

extern void exit(int)   __attribute__((noreturn));
extern void abort(void) __attribute__((noreturn));

Once tagged this way, the compiler can keep track of paths through the code and suppress errors that won‘t ever happen due to the flow of control never returning after the function call.

In this example, two nearly-identical C source files refer to an "exitnow()" function that never returns, but without the __attribute__tag, the compiler issues a warning. The compiler is correct here, because it has no way of knowing that control doesn‘t return.

$ cat test1.c
extern void exitnow();

int foo(int n)
{
        if ( n > 0 )
    {
                exitnow();
        /* control never reaches this point */
    }
        else
                return 0;
}

$ cc -c -Wall test1.c
test1.c: In function `foo‘:
test1.c:9: warning: this function may return with or without a value

But when we add __attribute__, the compiler suppresses the spurious warning:

$ cat test2.c
extern void exitnow() __attribute__((noreturn));

int foo(int n)
{
        if ( n > 0 )
                exitnow();
        else
                return 0;
}

$ cc -c -Wall test2.c
no warnings!

参考:

https://blog.csdn.net/qq_26093511/article/details/53306323

原文地址:https://www.cnblogs.com/sea-stream/p/11233641.html

时间: 2024-07-31 08:06:26

__attribute__((noreturn))的用法的相关文章

GUN C/C++ __attribute__ 用法 转

http://blog.csdn.net/mydo/article/details/3738336 GNUC的一大特色(却不被初学者所知)就是__attribute__机制.__attribute__可以设置函数属性(FunctionAttribute).变量属性(Variable Attribute)和类型属性(Type Attribute).__attribute__书写特征是:__attribute__前后都有两个下划线,并切后面会紧跟一对原括弧,括弧里面是相应的__attribute__

C语言中__attribute__的用法

http://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html http://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Variable-Attributes.html#Variable-Attributes GNU C的一大特色(却不被初学者所知)就是__attribute__机制.__attribute__可以设置函数属性(Function Attribute).变量属性(Variabl

转载--__attribute__ 你知多少?

作者:青春物语 链接:__attribute__ 你知多少? GNU C 的一大特色就是__attribute__ 机制.__attribute__ 可以设置函数属性(Function Attribute ).变量属性(Variable Attribute )和类型属性(Type Attribute ). __attribute__ 书写特征是:__attribute__ 前后都有两个下划线,并切后面会紧跟一对原括弧,括弧里面是相应的__attribute__ 参数. __attribute__

__attribute__你知多少(转)

转自:http://www.cnblogs.com/astwish/p/3460618.html GNU C 的一大特色就是__attribute__ 机制.__attribute__ 可以设置函数属性(Function Attribute ).变量属性(Variable Attribute )和类型属性(Type Attribute ). __attribute__ 书写特征是:__attribute__ 前后都有两个下划线,并切后面会紧跟一对原括弧,括弧里面是相应的__attribute__

attribute用法

attribute 用法 摘要: 在学习linux内核代码及一些开源软件的源码(如:DirectFB),经常可以看到有关__attribute__的相关使用.本文结合自己的学习经历,较为详细的介绍了__attribute__相关语法及其使用. ---------------------------------------------------------------- 声明: 此文为原创,欢迎转载,转载请保留如下信息 作者:聂飞(afreez) 北京-中关村 联系方式:[email prote

C之attribute用法

转自:http://www.cnblogs.com/astwish/p/3460618.html GNU C 的一大特色就是__attribute__ 机制.__attribute__ 可以设置函数属性(Function Attribute ).变量属性(Variable Attribute )和类型属性(Type Attribute ). __attribute__ 书写特征是:__attribute__ 前后都有两个下划线,并切后面会紧跟一对原括弧,括弧里面是相应的__attribute__

__attribute__ 你知多少?

引用原文地址是: http://www.cnblogs.com/astwish/p/3460618.html GNU C 的一大特色就是__attribute__ 机制.__attribute__ 可以设置函数属性(Function Attribute ).变量属性(Variable Attribute )和类型属性(Type Attribute ). __attribute__ 书写特征是:__attribute__ 前后都有两个下划线,并切后面会紧跟一对原括弧,括弧里面是相应的__attri

__ATTRIBUTE__ 知多少?

GNU C 的一大特色就是__attribute__ 机制.__attribute__ 可以设置函数属性(Function Attribute ).变量属性(Variable Attribute )和类型属性(Type Attribute ). __attribute__ 书写特征是:__attribute__ 前后都有两个下划线,并切后面会紧跟一对原括弧,括弧里面是相应的__attribute__ 参数. __attribute__ 语法格式为:__attribute__ ((attribut

【转】__ATTRIBUTE__ 你知多少

__ATTRIBUTE__ 你知多少? GNU C 的一大特色就是__attribute__ 机制.__attribute__ 可以设置函数属性(Function Attribute ).变量属性(Variable Attribute )和类型属性(Type Attribute ). __attribute__ 书写特征是:__attribute__ 前后都有两个下划线,并切后面会紧跟一对原括弧,括弧里面是相应的__attribute__ 参数. __attribute__ 语法格式为:__at