struct information

Programming, for all ages and all languages.
Post Reply
stonedzealot

struct information

Post by stonedzealot »

So I'm looking through some code, as I often do, and I realize that a lot of the structs have weird things after the elements are closed, but before the name ala:

Code: Select all

typedef struct
{
   word low_offset;   //low 8 bytes of handler offset
   word selector;      //handler's selector
   word settings;      //kind of entry
   word high_offset;   //high 8 bytes of handler offset
}  __attribute__ ((packed)) x86_interrupt;
The contents aren't a problem (of course) but what is __attribute__ ((packed))? Rather than answering this question straight out, I'd prefer if someone could point me to where I could get the information...
Tim

Re:struct information

Post by Tim »

Where to get the information: info gcc. Good luck.

It's easier if I (we) explain this particular one. The __attribute__(()) syntax is a gcc extension; packed means 'don't add padding to align the members of this struct'. It's probably not necessary on this structure, since gcc will (should) align those words on word boundaries, but it gets more important with, say, file system structures.
stonedzealot

Re:struct information

Post by stonedzealot »

Thanks alot! But in this case it is needed because these a written straight to the Interrupt Descriptor Tables and need to be accessed one after the other, with no spacing. Anyway, thanks for your help...
Tim

Re:struct information

Post by Tim »

In this case it's not needed because gcc aligns those words to 16-bit boundaries.

Roughly, the alignment rules are:
[o] chars are aligned to 1-byte boundaries
[o] shorts to 16 bits
[o] longs/ints to 32 bits
[o] doubles to 64 bits
[o] structures to the strictest of its field's alignment requirements (not sure about this)

So a structure consisting solely of words won't have any spacing between them.
stonedzealot

Re:struct information

Post by stonedzealot »

Ah, you've got me there. Thanks alot!
Post Reply