Anyone know where is the reference of C Structure Padding?
I mean, how do they pad data inside a structure?
Thanks in advance
Anyone know where is the reference of C Structure Padding?
I mean, how do they pad data inside a structure?
Thanks in advance
Last edited by audinue; 01-03-2009 at 05:05 AM.
Just GET it OFF out my mind!!
Look in the documentation for your compiler.
It's nothing to do with the standard.
The standard basically says two things
- the first element is aligned with the beginning of the structure
- the struct members are in the order written in the source code
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Does GCC/MSVC have it? I don't think soLook in the documentation for your compiler.
It's nothing to do with the standard.
Just GET it OFF out my mind!!
he probably meant, do they have some doc. For intel-like cpu, gcc generally tries to align fields on 32 bits (there is an option to align on 64 bits, this is probably the default for 64 bits arch). But see by yourself: create a structure with differents types and output the offsets. Anyway, the idea is that you should not have to care about that, except out of curiosity, otherwise, work with [byte] arrays if you have to control padding or include appropriate (non portable) directives or options to compact a structure ('info gcc' for details, if you do use gcc).What do you mean, "do they have it?"
By "pad" I presume you mean you want (for example) to make a 16 byte struct out of a 14 byte one. It would seem the best way to do this is with an extra char array, as it may be the only way to insure the exact number of bytes. I noticed with my compiler that if you use a short as a two byte pad, it may get further padded up to a full word. int unused:16 gives two bytes, but in this example (if you swapped char unused[2] for int unused), int unused:8 will also add 2 bytes and a just plain int unused actually adds 6 bytes (all the way to 20, according to sizeof(struct example)). I guess this is because the int is starting in the middle of a word and not on a word boundary.
Code:struct example { char one[7]; char two[7]; char unused[2]; }
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Your belief that there's not a padding byte between one and two, and between two and unused, is probably incorrect.
Edit: Actually, looking with offsetof, using int there's no padding between one and two, but there is between two and unused. (And there's only two bytes of padding, as 7+7+4=18.)
Last edited by tabstop; 01-03-2009 at 09:24 AM.
Hmm. But sizeof(struct example) will always return the correct value, correct?
(I assume these are compiler differences because this one is 16 bytes for me, which means the compiler didn't add anything. But as mentioned, if I use int unused, I get 20 bytes which I guess is 7+7+2+4).
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
There must be a formula to count that Anyone know?7+7+2+4
Just GET it OFF out my mind!!