#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Structure alignment or Padding


    I defined a structure as follows:
    Code:
    struct sample{
                int a;                           // 4  bytes
                char b;                       // 1 byte+ 3 bytes padding
                int c;                          // 4 bytes
                char c;                       //1 byte + 3 byte padding
    }
    When I tried sizeof(sample) , it printed 16 instead of 10 (int size on my system is 4 byte). That is acceptable, because compiler must have done structure padding
    in order to assign structure members, the addresses which are multiple of 4 thus
    making memory access faster.
    But now I modified my structure definition to:
    Code:
    struct sample{                        // what I expected this time
                short int a;                 //  2 bytes 
                short int b;                 //  2 bytes 
                short int c;                 //  2 bytes + 2 bytes padding
    }
    This time I expected sizeof(sample) to return 8 bytes, members a and b get
    first 4 bytes and then c is padded with 2 bytes to make it multiple of 4.
    BUT I WAS wrong. sizeof(sample) returns 6.Now I am tempted to think
    that Structure alignment is done with respect to the biggest size member
    in a structure (short int in this case), not according to the word size of the system???
    Help me out
    Note-By the way,My system is 32 bit.Is it right to assume word size of system to be 4 if size of int on system is 4??
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Yes, the alignment requirement of a struct is strongly determined by the alignment of the member with the most restrictive alignment.
    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
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    40
    Rep Power
    19
    Read:
    http://www.c-faq.com/struct/padding.html

    http://times.imkrisna.com/2011/11/counting-size-of-struct-in-c/
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by salem
    Yes, the alignment requirement of a struct is strongly determined by the alignment of the member with the most restrictive alignment.
    Thanks salem, But according to what you are telling,the following structure
    will have a size of 16 bytes,given double is 8 bytes and member b being
    most restrictive:
    Code:
    struct sample{                 //what i guess according to your response
                double b;           // 8 bytes
                char a;               // 1 byte + 7 bytes padding
    }
    But the size is 12 bytes , 8 bytes for double and then 1+3 bytes for char may be.
    Last edited by aliveashish; July 3rd, 2013 at 01:40 AM. Reason: typo
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    > given double is 8 bytes and member b being most restrictive:
    What are the alignment requirements of double on your machine?

    Note I said "alignment", not "size".
    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
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by salem
    > given double is 8 bytes and member b being most restrictive:
    What are the alignment requirements of double on your machine?
    Note I said "alignment", not "size".
    can you please elaborate, what do you mean by alignment requirment??
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Different processors have different data bus widths. Accessing memory is most efficient (read "faster") when you do it from an address that is evenly divisible by the data bus width. Such addresses are said to be "on a word boundary" and is considered to be properly aligned. Memory access is less efficient (read "slower") when accessing memory from an address that is not on a word boundary, because the processor has to do extra work which eats up extra clock cycles (ie, takes more time to do).

    Structure padding for alignment purposes is to align the fields onto addresses that are more efficient.

    But as long as you don't do anything that depends on the exact structure of the struct (eg, cross-platform apps that read and write whole structures from/to disk files or network packets), structure padding should be completely transparent. You shouldn't need to know exactly how much padding has been added where.
  14. #8
  15. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    ^^^^^
    Actually, this is one of my pet peeves that I think should be covered by the new C standards. There ought to be a standard way to tell the compiler to omit or specify padding, so one can send binary packets through a network or even read/write to a file.

    This is not even a cross-platform issue. I could write two programs on the same machine using the same C compiler: one program that writes out binary data to a file, another that reads it back from the file. Now, if I compile one of the programs with a different optimization level, the compiler may add or remove padding and therefore the program will write out data that cannot be read in by the other program. And this would be a very mysterious issue to debug because the code might have been working earlier before I flipped the optimization switch. There should be a way to unequivocally specify that padding should be omitted for a struct or union. Some compilers have a way to do this (e.g.) gcc has __attribute__((packed)) and #pragma pack to control padding, however these are all non-standard extensions. Would love it to become part of the official standard.
    Last edited by Scorpions4ever; July 3rd, 2013 at 03:01 PM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  16. #9
  17. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Even if you could standardise the padding and alignment, it would still leave the endian problem unsolved.
    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
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    structure padding


    Hi,

    -> padding is done like this

    #include<stdio.h>

    struct sample
    {
    short a; // 2 bytes,
    short b; // 2 bytes,
    short c; // 2 bytes,
    char d; // 1 byte + 1 byte padding
    double e; // 8 bytes


    }s1;

    void main()
    {
    int x;
    int a1,a2,a3,a4,a5;
    /* getting addresses of structure members with reference to 0 */
    a1=0;
    a2=(char *)&s1.b-(char *)&s1.a;
    a3=(char *)&s1.c-(char *)&s1.a;
    a4=(char *)&s1.d-(char *)&s1.a;
    a5=(char *)&s1.e-(char *)&s1.a;
    x=sizeof(struct sample);
    printf("a address of \na1 =%ld\na2 = %ld\na3 = %ld\na4 = %ld\na5 = %ld\n",a1,a2,a3,a4,a5);
    printf("\nsize = %d",x);

    }


    -> In 32 bit compiler memory is organized into 4 bytes each,
    next location starts at current + 4 bytes,

    -> In the above example a,b,c occupies 6 bytes,

    -> a,b occupies all the 4 byres in first level of memory

    -> c occupies only first 2 bytes of memory in 2nd level,
    -> d occupies one byte of memory in 2nd level, as the next
    variable e can not be accommodated on 1 byte left on 2nd level, the remaining byte of 2nd level is filled with zeros.

    -> e needs 8 bytes
    out of which 4 bytes are occupy in 3rd level
    and next 4 bytes are occupy in 4th level of memory,

    -> so 4 levels are completely filled.

    -> (4 levels) *(4 bytes) = 16 is the size of structure.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    structure padding (bit fields involved)


    #include<stdio.h>

    struct sample
    {
    int a:1;
    int b:1;
    int c:1;
    char d:4;
    int e:16;


    }s1;

    void main()
    {
    int x;

    x=sizeof(struct sample);

    printf("\nsize = %d",x);

    }



    -> a,b,c are single bits even though the are declare as integers we restricted their usage to 1 bit,

    ->char d is restricted to 4 bits,

    -> int e is restricted to 16 bits,

    totally 23 bits in 1st level of memory and remaining 9 bits are padded with 0s so we'll get 4 bytes as size of this structure.

IMN logo majestic logo threadwatch logo seochat tools logo