Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the __attribute__ keyword in GCC C?

Tags:

c

gcc

I am not clear with use of __attribute__ keyword in C.I had read the relevant docs of gcc but still I am not able to understand this.Can some one help to understand.

like image 947
reality displays Avatar asked Nov 19 '10 08:11

reality displays


People also ask

What is the use of __ attribute __ in C?

The __attribute__ directive is used to decorate a code declaration in C, C++ and Objective-C programming languages. This gives the declared code additional attributes that would help the compiler incorporate optimizations or elicit useful warnings to the consumer of that code.

What is __ attribute __ packed in C?

4.11 The __packed__ Attribute This attribute, attached to struct or union type definition, specifies that each member (other than zero-width bitfields) of the structure or union is placed to minimize the memory required. When attached to an enum definition, it indicates that the smallest integral type should be used.

What is attribute in GCC?

GCC Attributes are used to add various annotations to code outside of the basic source language that assist the compiler. There is also a user attribute that is not interpreted by the compiler but can be used by plugins and static analyses.

Which is a collection of the attributes of a variable?

Four attributes are currently defined for variables: aligned , mode , packed , and section . Other attributes are defined for functions, and thus not documented here; see Function Attributes. This attribute specifies a minimum alignment for the variable or structure field, measured in bytes.


Video Answer


2 Answers

__attribute__ is not part of C, but is an extension in GCC that is used to convey special information to the compiler. The syntax of __attribute__ was chosen to be something that the C preprocessor would accept and not alter (by default, anyway), so it looks a lot like a function call. It is not a function call, though.

Like much of the information that a compiler can learn about C code (by reading it), the compiler can make use of the information it learns through __attribute__ data in many different ways -- even using the same piece of data in multiple ways, sometimes.

The pure attribute tells the compiler that a function is actually a mathematical function -- using only its arguments and the rules of the language to arrive at its answer with no other side effects. Knowing this the compiler may be able to optimize better when calling a pure function, but it may also be used when compiling the pure function to warn you if the function does do something that makes it impure.

If you can keep in mind that (even though a few other compilers support them) attributes are a GCC extension and not part of C and their syntax does not fit into C in an elegant way (only enough to fool the preprocessor) then you should be able to understand them better.

You should try playing around with them. Take the ones that are more easily understood for functions and try them out. Do the same thing with data (it may help to look at the assembly output of GCC for this, but sizeof and checking the alignment will often help).

like image 163
nategoose Avatar answered Oct 20 '22 17:10

nategoose


Think of it as a way to inject syntax into the source code, which is not standard C, but rather meant for consumption of the GCC compiler only. But, of course, you inject this syntax not for the fun of it, but rather to give the compiler additional information about the elements to which it is attached.

You may want to instruct the compiler to align a certain variable in memory at a certain alignment. Or you may want to declare a function deprecated so that the compiler will automatically generate a deprecated warning when others try to use it in their programs (useful in libraries). Or you may want to declare a symbol as a weak symbol, so that it will be linked in only as a last resort, if any other definitions are not found (useful in providing default definitions).

All of this (and more) can be achieved by attaching the right attributes to elements in your program. You can attach them to variables and functions.

Take a look at this whole bunch of other GCC extensions to C. The attribute mechanism is a part of these extensions.

like image 23
Ziffusion Avatar answered Oct 20 '22 16:10

Ziffusion