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)
Attribute packed
Re:Attribute packed
__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
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
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.
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
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".
Code: Select all
struct foo
{
char c;
int i;
};
__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".
Every good solution is obvious once you've found it.
Re:Attribute packed
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
Did you try Google?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?
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
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
cheers Joe