views:

410

answers:

4

Hi, I was just wondering why and how is __attribute__ used in C programs.

thanks,

+2  A: 

One use is for enforcing memory alignment on variables and structure members. For example

float vect[4] __attribute__((aligned(16)));

Will ensure that vect will be placed on a 16 byte memory boundary. I do not know if that is a gcc-ism or more generally applicable.

The compiler will typically only aligned vect on a 4 byte boundary. With 16 byte alignment it can be used directly with SIMD load instructions where you'd load it up into a 128 bit registers that allows addition, subtraction, dot products and all manner of vector operations.

Sometimes you want alignment so that a structure can be directly overlaid onto memory-mapped hardware registers. Or it has to be aligned so the hardware can write into it directly used a direct memory access (DMA) mechanism.

George Phillips
+6  A: 

GCC attributes:

  • Function attributes described here
  • Variable attributes described here
  • Type attributes described here
qrdl
+1  A: 

Why is it used in C programs? To limit their portability.

It begins with a double-underscore, so it's in the implementor's namespace - it's not something defined by the language standard, and each compiler vendor is free to use it for any purpose whatsoever.

mlp
Uh, I don't think it's used specifically for the purpose of stunting portability.
GMan
@mpl So don't use it. Attributes allow you to squeeze more out of your hardware, but you are not forced to do it.
qrdl
@GMan: when it's used, that's one of the effects it will have.
mlp
@qrdl: I _don't_ use it, for precisely that reason.
mlp
mlp, that's not what you said. You said "it's used in C programs to limit their portability." That's simply false.
GMan
@GMan: The question did not mention GCC, or any specific compiler. '__attribute__' means whatever a compiler vendor wants it to mean. Use of any such feature has an explicit effect of limiting the portability of your program to only those compilers which provide that feature. While you may not intend to do so, you do limit portability by using it. My claim (which you paraphrased but marked as a quote, BTW) is not false -- not comprehensive, but not false.
mlp
+2  A: 

For what GCC and GCC-compatable compilers use __attribute__ most other compilers use #pragma directives. I think GCC's solution is better since the required behaviour of an unrecognised #pragma is to ignore it, whereas if you use a compiler that does not understand an __attribute__ specification, it will not compile - which is generally better, since you then know what you need to port.

Attributes specifications are used to specify aspects of types, data, and functions such as storage and alignment that cannot be specified using C. Often these are target specific, mostly they are non portable, certainly between compilers, and often between targets. Avoid their use except where absolutely necessary to the correct function of the code.

Clifford