Questions on structure padding

This is a discussion on Questions on structure padding within the C Programming forums, part of the General Programming Boards category; Hi All, I have following confusion: 1. When the structure is padded? Eg Code: struct A { char c; }; ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    139

    Questions on structure padding

    Hi All,

    I have following confusion:

    1. When the structure is padded?
    Eg

    Code:
    struct A
    {
         char c;
    };
    
    struct A c;
    
    sizeof(c);
    Size of the above struct is one

    But

    Code:
    struct B
    {
         int a
         char c;
    };
    
    struct B c;
    
    sizeof(c);
    Size of the following is 12

    How come first structure is not getting padded?

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The first structure fits perfectly with a C basic data type - a char. No padding needed. The second does not fit perfectly into a basic data type, so it needs it.

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, 11:14 PM
  2. Structure Padding in C
    By karthik537 in forum C Programming
    Replies: 3
    Last Post: 06-15-2011, 08:10 AM
  3. Structure padding
    By ShashiKantSuman in forum C Programming
    Replies: 4
    Last Post: 05-03-2011, 08:50 AM
  4. Structure padding
    By MK27 in forum C Programming
    Replies: 4
    Last Post: 12-15-2009, 02:25 PM
  5. Padding in Structure
    By ganesh bala in forum C Programming
    Replies: 11
    Last Post: 01-29-2009, 09:25 PM

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