1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Structure Padding

Discussion in 'C' started by Bibhas, May 29, 2008.

  1. Bibhas

    Bibhas New Member

    Joined:
    Apr 22, 2008
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Hi! I am a new member of this forum.:)
    I want know about Structure alignment & padding.
    Would anyone please tell me what is this & how to overcome the problem of padding?

    Thanx in advance
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    1
    Trophy Points:
    0
  3. Bibhas

    Bibhas New Member

    Joined:
    Apr 22, 2008
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Ok I understood about Padding. Thank u for ur help. But is it not a loss of memory? Because if I declare a structure like this
    struct myStruct
    {
    int a;
    char c1;
    char c2;
    char c3;
    char c4;
    char c5;
    };


    Then it takes 48 bytes of memory. But I want to allocate 13 bytes for it.

    So how to minimize the meory consumption?

    Please answer...
    :)
     
  4. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    1
    Trophy Points:
    0
    Try declaring char's before an int and that would reduce the memory consumption but it will not be 13 but very close to it
     
  5. Bibhas

    Bibhas New Member

    Joined:
    Apr 22, 2008
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Now I understood totally about Structure Padding. Thank you Shabbir for your help.

    Here I am trying to explain....
    If we declare a structure like this...
    struct S1
    {
    char C1;
    short S1;
    int I1;
    char C2;
    long L1;
    char C3;
    }S;

    & if we want to find the size of the structure S, we will get 6*8=48.
    But actually the size we expect is 1+2+4+1+8+1 = 17.
    This is happened,because the compiler add some extra bytes with allocated size of the variables. Briefly the size of the structure should be the highest size of a variable*total number of variables.
    So huge amount of memory is wasted.

    To minimize this problem...
    we can delcare the structure like this
    struct S1
    {
    long L1;
    int I1;
    short S1;
    char C1;
    char C2;
    char C3;
    }S;


    In this case the size would be..
    8+4+2+1+1+1+7=24

    But this is also not our desired result
    If we declare like this

    #pragma pack(1)

    struct S1
    {
    char C1;
    short S1;
    int I1;
    char C2;
    long L1;
    char C3;
    }S;


    Then the o/p would be 17
     
  6. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    1
    Trophy Points:
    0
    Looks good and thanks for sharing
     
  7. Bibhas

    Bibhas New Member

    Joined:
    Apr 22, 2008
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Just one thing I want to add for Structure Padding.....

    Compiler adds some extra bytes in the variable size, which is called Structure Alignment or Padding....

    Now the question is
    why does compiler add these extra bytes???
    The answer is:

    The size of an integer variable is 4 bytes & the starting address of it should be such address, which is devided by 4. e.g. In a structure the starting address of an integer would be 1000 or 2032 but cannot be 1001 or 2030
    Similarly for 'short'(e.g. In a structure the starting address of an short would be 1002 or 2030 but cannot be 1001 or 2031) & 'long' variable.

    Briefly the address of a variable in a structure should be
    Address%sizeof the variable = 0

    Thanks
    :)
     
  8. priyanka.ghosh

    priyanka.ghosh New Member

    Joined:
    Jul 21, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Nice article

    But I am not be able to understand why the address of a short variable in a structure is divided by two(2). Why this address could not be 1003 or 1111.
     
  9. Bibhas

    Bibhas New Member

    Joined:
    Apr 22, 2008
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    This is actually depends on Processor.
    To know more about this task, u need to understand about architecture of Micro Processor
     
  10. hkp819

    hkp819 New Member

    Joined:
    Dec 4, 2008
    Messages:
    59
    Likes Received:
    1
    Trophy Points:
    0
    The space allocated to any struct by compiler depends on the order in
    which the variables are defined.

    Try out the below example and see for urself
    struct a {
    int p;
    int q;
    int r;
    char s;
    char t;
    char u;
    };

    size of struct a = 16
    size of p+q+r+s+t = 15

    p (4) + q (4) = first 8 bytes
    r (4) + s (1) + t (1) + u (1) = next 8 bytes



    struct b {
    int p;
    char s;
    int q;
    char t;
    int r;
    char u;
    };

    size of struct b = 24 !!
    size of p+q+r+s+t = 15

    Reason :
    First compiler allocated 4 bytes to p then 1 byte to r, when it trys to
    allocate 4 more bytes to q this exceeds 8 bytes limit so it takes up the
    next byte and so on

    p (4) + s (1) = first 8 bytes // only 3 bytes remaining here....no space
    left to allocate q
    q (4) + t (1) = next 8 bytes
    r (4) + u (1) = last 8 bytes
     

Share This Page