Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sizeof operator

Reply
Thread Tools

sizeof operator

 
 
bintom
Guest
Posts: n/a
 
      02-15-2011
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
 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      02-15-2011
"bintom" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
 
 
 
bintom
Guest
Posts: n/a
 
      02-16-2011
> 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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      02-16-2011
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
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      02-16-2011
bintom <(E-Mail Removed)> 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.)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The sizeof operator : sizeof(++i) Kislay C Programming 10 10-19-2007 09:28 PM
#define ARR_SIZE sizeof(arr)/sizeof(arr[0]) Vinu C Programming 13 05-12-2005 06:00 PM
How to calculate size of an int without using the sizeof operator but using bitwise operator Manish_Ganvir C Programming 13 02-14-2005 07:24 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments