Alignment of variables in structs

Discussion in 'C++' started by Rick, Nov 29, 2007.

  1. Rick

    Rick Guest

    Given:

    struct x
    {
    int i;
    char c;
    }st_x;

    int size_of_struct = sizeof( struct x );
    int size_of_st_x = sizeof( st_x );

    struct x st_array[ 5 ];

    int size_of_st_array = sizeof( st_array );

    If an int is 32 bits and a char is 8 bits, are there any guarantees as
    to what size_of_struct, size_of_st_x, or size_of_st_array evaluate to?
    (they could be 5, 5, and 25, or 8, 8, and 40.)

    Are there any rules that specify whether compilers are required to
    align variables on any specific boundaries?

    I tried all of this on gcc on cygwin on Windows XP and got
    size_of_struct = 8, size_of_st_x = 8, and size_of_st_array = 40,
    rather than what one might expect at first glance, size_of_struct = 5,
    size_of_st_x = 5, and size_of_st_array = 25. Am I guaranteed to get
    that on all platforms?

    If there are any such guarantees, do any of you have a reference to a
    document that says so?

    Thanks...
     
    Rick, Nov 29, 2007
    #1
    1. Advertising

  2. Rick wrote:
    > Given:
    >
    > struct x
    > {
    > int i;
    > char c;
    > }st_x;
    >
    > int size_of_struct = sizeof( struct x );
    > int size_of_st_x = sizeof( st_x );
    >
    > struct x st_array[ 5 ];
    >
    > int size_of_st_array = sizeof( st_array );
    >
    > If an int is 32 bits and a char is 8 bits, are there any guarantees as
    > to what size_of_struct, size_of_st_x, or size_of_st_array evaluate to?
    > (they could be 5, 5, and 25, or 8, 8, and 40.)


    No, there is no guarantee about any particular number. However,
    there is a guarantee that

    size_of_struct == size_of_st_x
    size_of_struct * 5 == size_of_st_array

    > Are there any rules that specify whether compilers are required to
    > align variables on any specific boundaries?


    Nope.

    > I tried all of this on gcc on cygwin on Windows XP and got
    > size_of_struct = 8, size_of_st_x = 8, and size_of_st_array = 40,
    > rather than what one might expect at first glance, size_of_struct = 5,
    > size_of_st_x = 5, and size_of_st_array = 25. Am I guaranteed to get
    > that on all platforms?


    Nope.

    > If there are any such guarantees, do any of you have a reference to a
    > document that says so?


    It's all compiler-specific. See '#pragma align' (or some such).

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 29, 2007
    #2
    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. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    641
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    559
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    644
    Eric Sosman
    Oct 11, 2005
  4. Daniel Rudy
    Replies:
    15
    Views:
    1,402
    Keith Thompson
    Apr 10, 2006
  5. Rick

    Alignment of variables in structs

    Rick, Nov 29, 2007, in forum: C Programming
    Replies:
    3
    Views:
    645
    SM Ryan
    Nov 30, 2007
Loading...

Share This Page