size of object In C++

  • Follow


In C++ the size of object is the size of the data members in class. So
if it contains one integer data member it has size two bytes.
According to it if contains no data member its size should be Zero,
but in reality its size is one, Given by compiler.
0
Reply amandeep_dilse (5) 5/4/2004 4:39:07 AM

>>>>> "Aman" == Aman  <amandeep_dilse@yahoo.com> writes:

 Aman> In C++ the size of object is the size of the data members in class. So
 Aman> if it contains one integer data member it has size two bytes.
 Aman> According to it if contains no data member its size should be Zero,
 Aman> but in reality its size is one, Given by compiler.

In C++ object id is represented by its address (or what &object
returns). Since address points to memory, there should be something to point
at, there for size is atleast one. 

-- 
Arto V. Viitanen				                  av@cs.uta.fi
University of Tampere, Department of Computer Sciences
Tampere, Finland				      http://www.cs.uta.fi/~av/
0
Reply av111 (38) 5/4/2004 8:29:05 AM


Aman wrote:

> In C++ the size of object is the size of the data members in class.

That's not correct. There are things like alignment, and if you have
vitual functions, you probably get a vtable pointer in your class. So
in effect, sizeof(theobject) is often bigger than that of its data
members.

> So if it contains one integer data member it has size two bytes.
> According to it if contains no data member its size should be Zero,
> but in reality its size is one, Given by compiler.

It's a requirement of C++ that objects must have a size of at least 1,
because two objects cannot live at the same address. If you have an
array, each object's address is the first object's address plus
sizeof(theclass), but if that size is 0, all the array elements would
be at the same address.

0
Reply ramagnus (3485) 5/4/2004 9:23:20 AM

On 3 May 2004 21:39:07 -0700, amandeep_dilse@yahoo.com (Aman) wrote in
comp.programming:

> In C++ the size of object is the size of the data members in class.

What makes you think so?  This is not true.

> So
> if it contains one integer data member it has size two bytes.

No, if it contains one integer data member, it must be at least
sizeof(int) bytes.  On most popular C++ platforms today, that is 4
bytes of 8 bits each.  On some C++ implementations, it might have a
size of 2 bytes of 8 bits each.  On some C++ implementations it would
have a size of 1 byte of 16 bits, or 1 byte of 32 bits.

> According to it if contains no data member its size should be Zero,

According to what?  Try reading the ISO C++ standard, the official
definition of the language.  It does not state that the size of an
object of class type is the size of its members.  Nor does it define
that a class with no members has a size of 0 bytes.  In fact, it
prohibits objects from having a size of 0.  No such thing is allowed
in C++.

> but in reality its size is one, Given by compiler.

Perhaps you should buy a good book on C++ to clear up your
misunderstandings.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
0
Reply jackklein (3932) 5/5/2004 1:53:06 AM

3 Replies
24 Views

(page loaded in 0.111 seconds)


Reply: