sizeof operator

Discussion in 'C++' started by bintom, Feb 15, 2011.

  1. bintom

    bintom Guest

    class MyClass
    { int i;
    char ch;
    };

    int main()
    { cout << sizeof(MyClass); }


    Under Turbo C++, the above code gives sizeof 'MyClass' as 2 + 1 = 3
    bytes.

    But under Dev C++ and MS VC++, whereas it should give 4 + 1 = 5 bytes,
    it is showing 8 bytes. Why?

    Thanks in advance,

    Binoy Thomas
    bintom, Feb 15, 2011
    #1
    1. Advertising

  2. bintom

    Default User Guest

    "bintom" <> wrote in message
    news:...
    > class MyClass
    > { int i;
    > char ch;
    > };
    >
    > int main()
    > { cout << sizeof(MyClass); }
    >
    >
    > Under Turbo C++, the above code gives sizeof 'MyClass' as 2 + 1 = 3
    > bytes.
    >
    > But under Dev C++ and MS VC++, whereas it should give 4 + 1 = 5 bytes,
    > it is showing 8 bytes. Why?


    What makes you think that it "should" have that value? The implementation is
    allowed to pad between members or after the final member.



    Brian
    --
    Day 740 of the "no grouchy usenet posts" project
    Current music playing: None.
    Default User, Feb 15, 2011
    #2
    1. Advertising

  3. bintom

    bintom Guest

    > So that i is always correctly aligned. Consider an array of MyClass:

    > MyClass a[2];


    > if sizeof(MyClass) was 5 bytes then a[1].i would not be correctly aligned.



    Hi Leigh,

    I didn't get what you meant by 'correctly aligned'. Could u elaborate?

    Binoy Thomas
    bintom, Feb 16, 2011
    #3
  4. bintom

    Ian Collins Guest

    On 02/16/11 03:42 PM, Ruben Safir wrote:
    >
    > doesn't sizeof have the type of size_t?


    size_t is defined as

    "the unsigned integer type of the result of the sizeof operator"

    But how was that relevant to the original question?

    --
    Ian Collins
    Ian Collins, Feb 16, 2011
    #4
  5. bintom <> wrote:
    > But under Dev C++ and MS VC++, whereas it should give 4 + 1 = 5 bytes,
    > it is showing 8 bytes. Why?


    The size of a struct is not the sum of the sizes of its members.
    It's the minimum size that is required for the struct to be properly
    instantiated (for example as an array of such structs) without causing
    problems (or in some architectures, while still being as efficient as
    possible).

    For example, if you allocate an array of such structs, the amount of
    space allocated for each element must be such that each element starts
    from a properly aligned memory location. (Not doing so would cause bad
    inefficiency in some architectures, such as Intel's, and an outright
    crash in others, such as in UltraSparc architectures.)
    Juha Nieminen, Feb 16, 2011
    #5
    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. Derek
    Replies:
    7
    Views:
    24,305
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    616
    CBFalconer
    Apr 10, 2004
  3. Manish_Ganvir
    Replies:
    13
    Views:
    1,559
    Keith Thompson
    Feb 14, 2005
  4. Vinu
    Replies:
    13
    Views:
    1,387
    Lawrence Kirby
    May 12, 2005
  5. Kislay

    The sizeof operator : sizeof(++i)

    Kislay, Oct 18, 2007, in forum: C Programming
    Replies:
    10
    Views:
    673
    Peter Pichler
    Oct 19, 2007
Loading...

Share This Page