padding between variables in a structure

Discussion in 'C Programming' started by junky_fellow@yahoo.co.in, May 17, 2005.

  1. Guest

    Many times the compiler does the padding between two variables
    in a structure. For eg. if I have the structure as follows:

    struct sample {
    int var1;
    char var2;
    <--- padding of three bytes here
    int var3;
    }

    Why the compiler allocates 3 extra bytes between var2 and var3 ?
    I read some articles that say, the compiler always allocates
    the address to the variable which is a multiple of the size of
    the variable.
    for eg. if size of int is 4, the address allocated will always be a
    multiple of 4. Why is it so. If the processor can read a
    character from any address then why cannot an integer ?

    Does all compliers written for a particular architecture generate
    the same padding or allocate same space for a particular structure ?
    , May 17, 2005
    #1
    1. Advertising

  2. Michael Mair Guest

    wrote:
    > Many times the compiler does the padding between two variables
    > in a structure. For eg. if I have the structure as follows:
    >
    > struct sample {
    > int var1;
    > char var2;
    > <--- padding of three bytes here
    > int var3;
    > }
    >
    > Why the compiler allocates 3 extra bytes between var2 and var3 ?
    > I read some articles that say, the compiler always allocates
    > the address to the variable which is a multiple of the size of
    > the variable.
    > for eg. if size of int is 4, the address allocated will always be a
    > multiple of 4. Why is it so. If the processor can read a
    > character from any address then why cannot an integer ?


    Read FAQ 2.11 to 2.14, then ask again if there are still
    questions.

    > Does all compliers written for a particular architecture generate
    > the same padding or allocate same space for a particular structure ?


    No.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, May 17, 2005
    #2
    1. Advertising

  3. Russell Shaw Guest

    wrote:
    > Many times the compiler does the padding between two variables
    > in a structure. For eg. if I have the structure as follows:
    >
    > struct sample {
    > int var1;
    > char var2;
    > <--- padding of three bytes here
    > int var3;
    > }
    >
    > Why the compiler allocates 3 extra bytes between var2 and var3 ?
    > I read some articles that say, the compiler always allocates
    > the address to the variable which is a multiple of the size of
    > the variable.
    > for eg. if size of int is 4, the address allocated will always be a
    > multiple of 4. Why is it so. If the processor can read a
    > character from any address then why cannot an integer ?


    The address access granularity of a 32bit cpu is usually 4 bytes, because that's
    the basic integer size of the cpu. So even chars are commonly placed on 4-byte
    boundaries. Multiple chars may be packed into one int location, but that's
    implementation-specific iirc.

    > Does all compliers written for a particular architecture generate
    > the same padding or allocate same space for a particular structure ?


    Probably common, but isn't guaranteed.
    Russell Shaw, May 17, 2005
    #3
  4. Russell Shaw <rjshawN_o@s_pam.netspace.net.au> writes:
    [...]
    > The address access granularity of a 32bit cpu is usually 4 bytes,
    > because that's the basic integer size of the cpu. So even chars are
    > commonly placed on 4-byte boundaries. Multiple chars may be packed
    > into one int location, but that's implementation-specific iirc.


    On most modern 32-bit systems, the addressing granularity is 1 8-bit
    byte. Typically 8-bit objects are 8-bit aligned, 16-bit objects are
    16-bit aligned, and 32-bit objects are 32-bit aligned.

    This is *all* implementation-specific.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, May 17, 2005
    #4
  5. EventHelix.com, May 17, 2005
    #5
  6. Guest

    , May 18, 2005
    #6
  7. Flash Gordon Guest

    wrote:
    > EventHelix.com wrote:
    >
    >>Byte alignment and ordering issues are discussed in the following
    >>article:
    >>
    >>http://www.eventhelix.com/RealtimeMantra/ByteAlignmentAndOrdering.htm

    >
    > I had a look at the link suggested by you.
    > The link says "Structures greater than 16 bytes should be padded
    > to 16 byte boundary".
    > Any idea of why this is being done ?


    That is explained under the heading "Why Restrict Byte Alignment?"

    A further point is that some processors will not let you access
    non-aligned data.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, May 18, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amarendra

    Structure padding.

    Amarendra, Jun 21, 2004, in forum: C Programming
    Replies:
    13
    Views:
    9,557
    Ralmin
    Jun 22, 2004
  2. phoenix

    structure padding

    phoenix, Mar 11, 2005, in forum: C Programming
    Replies:
    1
    Views:
    394
    Peter Nilsson
    Mar 11, 2005
  3. Stephen Mayes

    structure padding

    Stephen Mayes, May 20, 2005, in forum: C Programming
    Replies:
    5
    Views:
    427
    Dave Thompson
    May 31, 2005
  4. Replies:
    9
    Views:
    25,228
    Lal Bahadur Singh
    Nov 11, 2011
  5. Kislay

    Size of a structure : Structure Padding

    Kislay, Oct 1, 2007, in forum: C Programming
    Replies:
    15
    Views:
    932
    clinuxpro
    Jul 13, 2011
Loading...

Share This Page