Why and how is __attribute__
used in GNU C programs?
3 Answers
For what GCC and GCC-compatible compilers use __attribute__
most other compilers use #pragma
directives.
I think GCC's solution is better since the required behavior 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.
Attribute 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 it is absolutely necessary to use correct functions of code.
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.

- 4,564
- 27
- 25
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.
Edit: Why is it used in GNU C programs? See the other answers that address this.

- 809
- 7
- 21
-
14Uh, I don't think it's used specifically for the purpose of stunting portability. – GManNickG Sep 22 '09 at 08:27
-
2@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 Sep 22 '09 at 10:06
-
@GMan: when it's used, that's one of the effects it will have. – mlp Sep 22 '09 at 21:39
-
@qrdl: I _don't_ use it, for precisely that reason. – mlp Sep 22 '09 at 21:41
-
9mlp, that's not what you said. You said "it's used in C programs to limit their portability." That's simply false. – GManNickG Sep 23 '09 at 01:29
-
1@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 Oct 01 '09 at 04:14
-
7No, it is strictly speaking false. It is in no way used to limit portability; limits to portability occur because of it, but it is never used in order to limit portability. This is a subtle, but quite important, difference. – Alice Dec 13 '14 at 10:23
-
4@mlp Even if using an attribute limited portability, that still does not in any way imply that attributes are USED or EXIST to limit portability. A side effect is not necessarily the intended effect. – Alice Dec 16 '14 at 23:17
-
5I stumbled upon this question because I too (shamefully) googled `__attribute__` after having briefly studied the C-source for upstart. I am still find new things to know about the C languages and all the differences that exists in different compiler implementations. I now find attributes new, interesting, and noteworthy. I would gladly have used them in the future for the features they do seem to give. Perhaps, after having read this answer, I will think twice though (if I care about portability, which - on occasion - I might not). – Ole Thomsen Buus Jan 13 '15 at 08:05
-
They might have the effect of limiting portability but your claim that that is the reason they are used sounds completely unfounded. Do you actually mean that developers use this for the *purpose* of limiting portability? Can you elaborate that or show an example??? – Kröw Apr 17 '23 at 21:27
-
@Kröw (and all who have downvoted my answer here over the last 9 years) as originally posed 13 years ago, the question asked about C, not **GNU C**. `__attribute__` *does not exist* in C - it *can only exist* in distinct implementations. If you write C code containing *any* Implementation Defined Stuff, then *by definition* (even before that Stuff does its thing) you have limited the portability of your code to compilers which implement that Stuff in the way you desire. Your desire for what the Stuff provides exceeds your desire for portability to other compilers. You can't have it all. – mlp Apr 24 '23 at 22:59
-
@mlp You have stated that before. If you do have any examples of someone deliberately using `__attribute__` for the express purpose of limiting portability in their program, I would be interested to know the context behind it; (I was thinking you may have some odd, but thoughtful anecdotal example of such), but judging by your most recent comment, that is not what `__attribute__` is used for, and so the answer you gave seems to just be making a false claim... – Kröw Apr 25 '23 at 01:38
-
@mlp (Please note that there is a distinction between the statement "`__atribute__` is used to limit portability" and the statement "`__attribute__` has the effect of limiting portability." If you have a serious, practical example of the former, it would make for an interesting answer to this question.) – Kröw Apr 25 '23 at 01:40