Thread: Questions on structure padding

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

    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, 10:14 PM
  2. Structure Padding in C
    By karthik537 in forum C Programming
    Replies: 3
    Last Post: 06-15-2011, 07:10 AM
  3. Structure padding
    By ShashiKantSuman in forum C Programming
    Replies: 4
    Last Post: 05-03-2011, 07: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