Thread: Padding in Structure

  1. #1
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Thumbs up Padding in Structure

    Size of below structure is 32 bytes, how it is 32 bytes?
    can you explain me?

    2+2+2+2+4+8+8=28 bytes


    Code:
    #pragma pack(push, 8)
    struct MY_STRUCT
    {
    char a[2];
    short b;
    short c;
    short d;
    int e;
    long long x;
    long long y;
    };
    what will pragma pack directive do ? will this changes alignment?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    A long long would normally start on a 8-byte boundary, so it couldn't start at byte 12. pragma pack, if the compiler understands it in the first place, would suggest removing all "extra" padding bytes in the structure.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    a, b, c, d, can pack, for a total of 8 bytes. This is an okay alignment for int e, so it packs there, for a total of 12 bytes. This is NOT an okay alignment for long long f, so 4 bytes are inserted to align it at 16 bytes. 16 bytes plus the two long longs makes 32, which is sufficiently aligned for the largest alignment in the struct (the alignment of long long which is 8), so no further pad bytes are used, leading to a grand total of 32 bytes.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58
    Thanks tabstop n brewbuck :-)

  5. #5
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58
    This is another one... I am getting 32 as size of struct....

    but , i found only 28..

    4+(padding)4+8+4+1+(padding)3+4=28... where i was wrong??

    Code:
    struct MY_STRUCT
    {
    
    	int ctrl1;
    	double flag;
    	int ctrl2;
    	char flag2;
    	int ctrl3;
    };

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    http://www.cplusplus.com/reference/c.../offsetof.html
    Use this to explore your struct alignment questions.
    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.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    order the members of the struct from longest types to shortest if you want to acheive more compact form without using pack
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58
    Thanks salem n vart..

    i checked with offsetof...

    its showing last one=> int offset is at 24...

    this means size of struct should be 28...

    But i m getting 32...

    Code:
    Anyway thanks for offsetof ... New function i learnt today

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    The largest element is the double, which is 8 bytes, so the compiler adds 4 bytes to make the size of the whole struct a multiple of 8.

    If the size is 28, and you stack 2 of them in an array, the double element in the second one won't be aligned to a 8-byte boundary (28+8).

  10. #10
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58
    Thanks Cyberfish 4 ur help...

    i think There are lot of things we have to consider in alignment ...

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    If you are using that pragma directive with an 8 byte alignment then 32 is the right answer as in
    Code:
    struct MY_STRUCT
    {
    
    	int ctrl1;   /* 8 bytes */
    	double flag; /* 8 bytes */
    	int ctrl2;   /* this one and the next can fit into a 8 byte word */
    	char flag2;  
    	int ctrl3;   /* 8 bytes */
    };
    Last edited by itCbitC; 01-29-2009 at 10:31 AM.

  12. #12
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Talking

    Thanks itcbitc

    Now i understood what Pragma directive will do

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, 10:25 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM