Thread: C Structure Padding

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    C Structure Padding

    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!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    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.

  3. #3
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Look in the documentation for your compiler.
    It's nothing to do with the standard.
    Does GCC/MSVC have it? I don't think so
    Just GET it OFF out my mind!!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by audinue View Post
    Does GCC/MSVC have it? I don't think so
    What do you mean, "do they have it?" If you mean do they pad structures, then the answer is assuredly "yes". If you mean do they pad structures the same way, then maybe maybe not.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    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];
    }
    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.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    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.)
    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

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    What do you mean, "do they have it?"
    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).

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The only way to ensure the struct has the right size is to disable padding and that is a compiler-specific option that differs between compilers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it will return the actual size.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    7+7+2+4
    There must be a formula to count that Anyone know?
    Just GET it OFF out my mind!!

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by audinue View Post
    There must be a formula to count that Anyone know?
    It's called offsetof in stddef.h.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It would make more sense that it aligned the odd chars on a DWORD boundary, though. At least to me.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Elysia View Post
    It would make more sense that it aligned the odd chars on a DWORD boundary, though. At least to me.
    Wild guessing: Presumably this is so, if someone like cus comes along who wants to copy one struct into another struct, and the struct that person is copying has just the two char arrays in it (and not everything else), then the alignments still match. After all, chars don't align on a DWORD.

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Nope, it's definitely (at least on this machine) 7+7+2+4.
    Yes this is possible, bytes and byte arrays are aligned by definition, so no padding is required here, only objects greater than 1 byte (and accessed as such) have to be aligned.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to handle structure padding
    By cdalten in forum C Programming
    Replies: 8
    Last Post: 03-17-2006, 09:29 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Structure Padding, pragma pack...
    By P.Phant in forum C Programming
    Replies: 4
    Last Post: 06-04-2003, 05:56 AM
  4. Detecting Structure Padding
    By johnnie2 in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2003, 10:25 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM