Skip to content
Search

GCC:Attribute

lastmod 2021-11-05created 2016-11-165 edits

In GNU C, you can declare things about functions called in your program that make the compiler optimize and check them more carefully.

__attribute__ lets us give special attributes to a function when we declare it. This __atribute__ is followed by an attribute argument enclosed in double parentheses. We can use the following attribute keywords with __ added before and after them. This lets these keywords be used in header files regardless of any macros defined with the same name.

Types

Variable Attribute
http://gcc.gnu.org/onlinedocs/gcc-4.6.1/gcc/Variable-Attributes.html#Variable-Attributes
Type Attribute
http://gcc.gnu.org/onlinedocs/gcc-4.6.1/gcc/Type-Attributes.html#Type-Attributes
Function Atrribute
-

noreturn

A few standard library functions, such as abort and exit, cannot return. GCC knows this automatically. Some programs define their own functions that never return. We can tell the compiler about this by declaring noreturn.

void fatal() __attribute__((noreturn));
void fatal( /* ... */ )
{
/* ... */ /* Print error message. */ /* ... */
exit(1);
}

The noreturn keyword tells the compiler that the fatal function cannot return. It can then optimize without regard to what would happen if fatal ever did return. This makes slightly better code, and more importantly, it helps avoid spurious warnings about uninitialized variables. It makes no sense for a noreturn function to have a return type other than void.

See also

Favorite site

author Zer0 sitemap