Attribute packed

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
OSMAN

Attribute packed

Post 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)
Crazed123

Re:Attribute packed

Post 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.
Warrior

Re:Attribute packed

Post 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.
Cjmovie

Re:Attribute packed

Post 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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Attribute packed

Post by Solar »

When you declare

Code: Select all

struct foo
{
    char c;
    int i;
};
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".
Every good solution is obvious once you've found it.
0Scoder
Member
Member
Posts: 53
Joined: Sat Nov 11, 2006 8:02 am

Re:Attribute packed

Post 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?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Attribute packed

Post 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.
JoeKayzA

Re:Attribute packed

Post 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
Post Reply