[gcc 4.4.3, ubuntu 10.04] Structure Padding

Discussion in 'C' started by tukki, Aug 20, 2010.

  1. tukki

    tukki New Member

    Joined:
    Aug 14, 2010
    Messages:
    10
    Likes Received:
    1
    Trophy Points:
    0
    Hello Everyone. I need a little in understanding the padding of structures by a compiler.
    The code is:
    Code:
    #include<stdio.h>
    
    struct a{
    int a;
    char c; };
    
    struct b{
    char i;
    char j; };
    
    void main()
    {
    struct a a1;
    struct b b1;
    
    printf("a1: %d\n",sizeof(a1));
    printf("a1.a: %d\n",sizeof(a1.a));
    printf("a1.c: %d\n",sizeof(a1.c));
    
    printf("b1: %d\n",sizeof(b1));
    printf("b1.i: %d\n",sizeof(b1.i));
    printf("b1.j: %d\n",sizeof(b1.j));
    }
    And here is the output:
    Code:
    a1: 8       //its of size 8 !!!
    a1.a: 4    //while its members total upto 5 (4+1) !=8!
    a1.c: 1    
    
    b1: 2   //This here shows expected behaviour-> 2=1+1
    b1.i: 1   
    b1.j: 1
    #1.> What are the rules for such padding.
    #.> What is the arrangement of variables in the structure.

    Thanks
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Memory is something like a rectangular chunk and so you actually allocate it like this

    int
    0000
    char
    0---

    the three --- for the ease of access

    If you declare a float then the size of each variable will be of float.
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Depends on the CPU. Some processors can't access multibyte types at all addresses so two-byte types need to be at even addresses, four-byte types at addresses divisible by 4 etc. Or they can but it takes longer because basically they can't so they get round that by doing two memory reads and swapping the bytes around. So the padding is added so that the CPU can address memory at addresses divisible by N. It's not something you normally need to worry about as a programmer as it's all handled automatically; the only time it's an issue is when you're poking around inside a padded structure with a pointer with a resolution of one byte, but there is very little need for that.
     
  4. tukki

    tukki New Member

    Joined:
    Aug 14, 2010
    Messages:
    10
    Likes Received:
    1
    Trophy Points:
    0
    @shabbir: Thanks for replying.

    @xpi0t0s: Thanks for replying.

    I searched google and found some useful links, but am unable to post them here.

    Thanks
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You will be able to post them after complete 10 posts and this has been done to stop spamming. Else you can edit the http and www part from the url and post them
     
  6. tukki

    tukki New Member

    Joined:
    Aug 14, 2010
    Messages:
    10
    Likes Received:
    1
    Trophy Points:
    0
    @shabbir: Oh alright, here they are:
    Link#1. //bytes.com/topic/c/answers/543879-what-structure-padding
    link#2. //clinuxpro.com/Cprogramming/structurepadding.php

    and one more thing--to control padding u can use #pragma directive specifically #pragma pack( ). Here is one rest can be found by google...
    //publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/compiler/ref/rnpgpack.htm

    Hope they are useful.
    Thanks
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice