Structure padding

This is a discussion on Structure padding within the C Programming forums, part of the General Programming Boards category; Am I right to assume that compilers will pad out to a multiple of 4 on x86 platforms? Is there ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Structure padding

    Am I right to assume that compilers will pad out to a multiple of 4 on x86 platforms?

    Is there a way, such a macro, to determine the size?
    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

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    This is compiler dependent, but usually on x86, a char will be 1-byte aligned, shorts 2-byte aligned, ints/floats 4-byte aligned.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Epy
    Epy is online now
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    943
    By default, I don't know.

    I know you can specify an alignment with #pragma pack down to a 1-byte boundary up to a 16-byte boundary (powers of 2).

    Data structure alignment - Wikipedia, the free encyclopedia <-- looks like it differs by the type within the struct
    pack
    Structure-Packing Pragmas - Using the GNU Compiler Collection (GCC)

    Hopefully something there will point you in the right direction.

  4. #4
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    I assume most compilers do. But I am pretty sure you can cook up a way to investigate this.

    First idea that comes to my mind, is to try creating a struct which obviously would be padded if padding is done to 4 byte boundary. For example:

    typedef struct SPadTest
    {
    char byte1;
    char byte2;
    unsigned long long int eightbytesPropably;
    char yetAnotherByte;
    }SPadTest;

    Now sizeof(SPadTest) should give you an idea.

    and

    Code:
    unsigned int i;
    SPadTest test;
    memset(&test,0,sizeof(SPadTest));
    test.byte1=0xFF;
    test.byte2=0xFF;
    test.eightbytesPropably=0xFFFFFFFFFFFFFFFF;
    test.yetAnotherByte=0xFF;
    
    for(i=0;i<sizeof(SPadTest);i++)
    {
        printf("0x%x",*(((char *)&test)+i));
    }
    should give you a strong hint.

    There's also #pragma(s) to set desired padding, like #pragma pack() - but that probably is compiler dependant too.

    So if you need to ensure padding with compilers you do not own, then I guess it is best to try inventing reliable way to detect padding using sizeof() and some test structs. (Or then someone will tell you the macro/exact answer - although I have not heard of such macro - especially any such macro that would reliably work on all compilers )

  5. #5
    Epy
    Epy is online now
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    943
    There's also #pragma(s) to set desired padding, like #pragma pack() - but that probably is compiler dependant too.
    Both GCC and VS seem to support #pragma pack, don't know about others.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Structure Padding
    By audinue in forum C Programming
    Replies: 20
    Last Post: 07-12-2011, 10:14 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, 09:25 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM

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