Structures

Discussion in 'C++' started by ben, Mar 14, 2005.

  1. ben

    ben Guest

    Hi guyz,
    I have a small doubt in the following program.Upon
    execution,the program shows that size of structure variable is 20
    whereas size of its individual elements is 4,4,4,1 and 4.If we add the
    size of individual elements we get the total as 17.Then how come the
    size of the structure varialble is 20?(The program gives the same ans.
    on linux as well as WINDOWS(VC++)).

    #include<iostream>
    using namespace std;

    struct Column
    {
    char *fieldName;
    int fieldType;
    void *defaultValue;
    void *value;
    bool nullFlag;
    };

    int main()
    {
    Column a;
    a.nullFlag=false;
    cout<<sizeof(a)<<endl;
    cout<<sizeof(a.fieldName)<<endl;
    cout<<sizeof(a.fieldType)<<endl;
    cout<<sizeof(a.defaultValue)<<endl;
    cout<<sizeof(a.nullFlag)<<endl;
    cout<<sizeof(a.value)<<endl;
    return 0;
    }
    Program output:20
    4
    4
    4
    1
    4
    bye,
    ben
    ben, Mar 14, 2005
    #1
    1. Advertising

  2. ben

    Rolf Magnus Guest

    ben wrote:

    > Hi guyz,
    > I have a small doubt in the following program.Upon
    > execution,the program shows that size of structure variable is 20
    > whereas size of its individual elements is 4,4,4,1 and 4.If we add the
    > size of individual elements we get the total as 17.Then how come the
    > size of the structure varialble is 20?(The program gives the same ans.
    > on linux as well as WINDOWS(VC++)).


    The compiler is allowed to put in padding bytes to ensure the members are
    properly aligned. On many systems, e.g. a 4 byte integer in memory can only
    be accessed if its address is a multiple of 4. Some systems allow accessing
    unaligned variables, but at the price of a slower access. So what happens
    is that after the bool member, 3 padding bytes are inserted, so that in the
    case of an array of Column, the first member of the next array element is
    again aligned at a 4 byte boundary.

    > #include<iostream>
    > using namespace std;
    >
    > struct Column
    > {
    > char *fieldName;
    > int fieldType;
    > void *defaultValue;
    > void *value;
    > bool nullFlag;
    > };
    >
    > int main()
    > {
    > Column a;
    > a.nullFlag=false;
    > cout<<sizeof(a)<<endl;
    > cout<<sizeof(a.fieldName)<<endl;
    > cout<<sizeof(a.fieldType)<<endl;
    > cout<<sizeof(a.defaultValue)<<endl;
    > cout<<sizeof(a.nullFlag)<<endl;
    > cout<<sizeof(a.value)<<endl;
    > return 0;
    > }
    > Program output:20
    > 4
    > 4
    > 4
    > 1
    > 4
    > bye,
    > ben
    Rolf Magnus, Mar 14, 2005
    #2
    1. Advertising

  3. ben wrote:

    > Hi guyz,
    > I have a small doubt in the following program.Upon
    > execution,the program shows that size of structure variable is 20
    > whereas size of its individual elements is 4,4,4,1 and 4.If we add the
    > size of individual elements we get the total as 17.Then how come the
    > size of the structure varialble is 20?(The program gives the same ans.
    > on linux as well as WINDOWS(VC++)).


    In addition to what Rolf said, remember this rule:
    The size of a structure may be greater than the sum
    of the sizes of the members.

    Also note that because of the padding, structures
    should not be written in binary to streams, especially
    for retrieving at a later date. The better method
    is to create binary reading and writing methods
    which write out the individual members.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
    Thomas Matthews, Mar 14, 2005
    #3
  4. ben

    ben Guest

    Thanks.This is something i never came across in any C++ book.Thanks
    again.
    ben, Mar 15, 2005
    #4
    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. Pedro Claro
    Replies:
    2
    Views:
    2,447
    Mike Treseler
    Jul 16, 2003
  2. Olaf Petzold

    unconstrained structures

    Olaf Petzold, Aug 29, 2005, in forum: VHDL
    Replies:
    4
    Views:
    572
    Mike Treseler
    Aug 30, 2005
  3. Dennis Gavrilov
    Replies:
    1
    Views:
    1,441
    Dennis Gavrilov
    Jul 24, 2003
  4. tweak
    Replies:
    14
    Views:
    2,769
    Eric Sosman
    Jun 11, 2004
  5. Alfonso Morra
    Replies:
    11
    Views:
    703
    Emmanuel Delahaye
    Sep 24, 2005
Loading...

Share This Page