C Structure Padding

This is a discussion on C Structure Padding within the C Programming forums, part of the General Programming Boards category; Anyone know where is the reference of C Structure Padding? I mean, how do they pad data inside a structure? ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,185
    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
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    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).

  6. #6
    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

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    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.

  9. #9
    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

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    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,185
    Quote Originally Posted by MK27 View Post
    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).
    Right. Reading it again, I think I misinterpreted what you said -- I thought you meant six bytes of padding, where you meant "adds six bytes" in that the int was four and there were two bytes of padding.

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

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Quote Originally Posted by MK27 View Post
    (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).
    Perhaps it is 8 + 8 + 4?
    However, we can say that most compilers will strive to align variables to boundaries, typically 4 or 8 bytes.
    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.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Elysia View Post
    Perhaps it is 8 + 8 + 4?
    However, we can say that most compilers will strive to align variables to boundaries, typically 4 or 8 bytes.
    Nope, it's definitely (at least on this machine) 7+7+2+4. I haven't tried it in VS.

Page 1 of 2 12 LastLast
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, 07:39 AM
  3. Structure Padding, pragma pack...
    By P.Phant in forum C Programming
    Replies: 4
    Last Post: 06-04-2003, 06: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21