Page 1 of 1
Attribute packed
Posted: Fri Sep 23, 2005 2:46 pm
by OSMAN
Hi.
What does __attribute__( (packed) ) actually do? Is it a service of the compiler?
When there is a struct with some exact my-ordering bytes and words and bits, why must it be packed? In asm it musn't, right?! (I mean in IDT with C-language)
Re:Attribute packed
Posted: Fri Sep 23, 2005 3:11 pm
by Crazed123
__attribute(packed)__ means that the compiler doesn't insert padding in between struct fields in order to make them line up on 32-bit boundaries. Thus we say that the struct items are "packed" together.
Re:Attribute packed
Posted: Sat Sep 24, 2005 9:24 am
by Warrior
Used when for example when the size that the structure comes out to be must be a set size, padding would then ruin that.
Re:Attribute packed
Posted: Sun Sep 25, 2005 11:31 am
by Cjmovie
Also, " __attribute__( (packed) )" is specific to GCC compilers.
And it packs it to 32-bit boundary for optimization, as the Intel-32bit processors (and AMD, as it would go) are best when moving their bus width as a whole, which is 32-bits.
Re:Attribute packed
Posted: Mon Sep 26, 2005 12:59 am
by Solar
When you declare
a C compiler is free to add three bytes of padding after c, to have i properly aligned. The speed increase is usually worth the "wasteage" of space (and most structs are designed big-type-first anyway).
__packed__ (or the command line option -fpack-struct in later GCC versions, if you want to keep your code clean of GCC lingo) is for those cases where you don't want to declare
some struct, but an
exact struct defined by something external, like in a device driver or some cross-platform protocol where alignment might differ from what the compiler does "natively".
Re:Attribute packed
Posted: Mon Sep 26, 2005 10:20 am
by 0Scoder
I couldn't find this anywhere else in the forum, so I'm asking it here: Is there an _align_ attribute to get things sligned into pages, and if so how do I use it?
Re:Attribute packed
Posted: Tue Sep 27, 2005 1:05 am
by Candy
OScoder wrote:
I couldn't find this anywhere else in the forum, so I'm asking it here: Is there an _align_ attribute to get things sligned into pages, and if so how do I use it?
Did you try
Google?
Fourth hit:
GCC type attributes
It explains as follows:
aligned (alignment)
This attribute specifies a minimum alignment (in bytes) for variables of the specified type. For example, the declarations:
struct S { short f[3]; } __attribute__ ((aligned (8)));
typedef int more_aligned_int __attribute__ ((aligned (8)));
force the compiler to insure (as far as it can) that each variable whose type is struct S or more_aligned_int will be allocated and aligned at least on a 8-byte boundary. On a SPARC, having all variables of type struct S aligned to 8-byte boundaries allows the compiler to use the ldd and std (doubleword load and store) instructions when copying one variable of type struct S to another, thus improving run-time efficiency.
Re:Attribute packed
Posted: Tue Sep 27, 2005 8:13 am
by JoeKayzA
I can hardly imagine that you didn't find it elsewhere on the forum, I can remember of 2 times I wrote about the 'align' attribute...
cheers Joe