Structure Query....

Discussion in 'C Programming' started by Sachin, Sep 8, 2004.

  1. Sachin

    Sachin Guest

    typdef struct
    {
    int i;
    char ch;
    }str;
    str str_var;
    char x, y;
    main()
    {
    //do nothing
    }

    In the above piece of code the structure needs a padding of 3 more
    bytes (the total size of the structure variable being 8 bytes). Now
    my question is, wont the compiler append x & y to the end of structure
    and adds just 1 byte as the padding?
    In my case I use MSVC compiler. When I checked the size of obj in the
    map file the obj size was 12 bytes....shouldnt it be 8 bytes ideally??

    Thanks in advance...
    Regds
    Sachin
     
    Sachin, Sep 8, 2004
    #1
    1. Advertising

  2. Sachin

    -berlin.de Guest

    Sachin <> wrote:
    > typdef struct
    > {
    > int i;
    > char ch;
    > }str;
    > str str_var;
    > char x, y;
    > main()


    int main( void )

    > {
    > //do nothing
    > }


    > In the above piece of code the structure needs a padding of 3 more
    > bytes (the total size of the structure variable being 8 bytes). Now
    > my question is, wont the compiler append x & y to the end of structure
    > and adds just 1 byte as the padding?


    First of all, the compiler is entitled to add as much padding as
    it wants, so you should never rely on a certain value. Second,
    you can't know if the compiler will put x and y at a certain place
    in relation to the structure, it could put the before or after the
    structure or somewhere completely different. Third, even if both
    these variable would come directly after the structure in memory
    the structure would still have its full padding - if different
    padding would be used dependening on circumstances the value of
    'sizeof( struct str )' wouldn't be welldefined. And imagine what
    would happen then if you define another such structure and do

    memcpy( &str_var, &another_str_struct, sizeof( struct str ) );

    > In my case I use MSVC compiler. When I checked the size of obj in the
    > map file the obj size was 12 bytes....shouldnt it be 8 bytes ideally??


    No idea what "size of obj in the map file" is, but you can simply
    get the size with the sizeof operator. But the compiler is still
    free to add as much padding as it likes to (or thinks necessary).
    If that's a different value from what you think is necessary you
    have to discuss that with the people that wrote the compiler, it
    is nothing the C standard specifies.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Sep 8, 2004
    #2
    1. Advertising

  3. Sachin

    Ben Pfaff Guest

    (Sachin) writes:

    > typdef struct
    > {
    > int i;
    > char ch;
    > }str;
    > str str_var;
    > char x, y;
    > main()
    > {
    > //do nothing
    > }
    >
    > In the above piece of code the structure needs a padding of 3 more
    > bytes (the total size of the structure variable being 8 bytes). Now
    > my question is, wont the compiler append x & y to the end of structure
    > and adds just 1 byte as the padding?


    No. Then the following:
    memset (&str_var, 0, sizeof str_var);
    would clear x and y as well as str_var.
    --
    "A lesson for us all: Even in trivia there are traps."
    --Eric Sosman
     
    Ben Pfaff, Sep 8, 2004
    #3
    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. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    792
    Martin Ambuhl
    Oct 26, 2004
  2. Leo Nunez
    Replies:
    3
    Views:
    1,298
    Neil Kurzman
    Feb 9, 2005
  3. Replies:
    2
    Views:
    642
  4. Replies:
    9
    Views:
    25,539
    Lal Bahadur Singh
    Nov 11, 2011
  5. meisterbartsch
    Replies:
    2
    Views:
    808
    meisterbartsch
    Jun 12, 2007
Loading...

Share This Page