why is size of object of empty class is 1 byte

I

ishekara

Hi all,

An object of a class A which has no member variables and no member
functions, still the size of the object is 1 byte, if there is a byte member
variable then also the size of the object is 1 byte. Could anyone tell me
the reason behind this.

Thanks
ishekara
 
S

Shan

An object of a class A which has no member variables and no member
functions, still the size of the object is 1 byte, if there is a byte member
variable then also the size of the object is 1 byte. Could anyone tell me
the reason behind this.

There is no requirement in the C++ standard that an empty object should
have one byte of memory aoocupied. It is purely based on the
implementation.
 
M

Mike Hewson

ishekara said:
Hi all,

An object of a class A which has no member variables and no member
functions, still the size of the object is 1 byte, if there is a byte member
variable then also the size of the object is 1 byte. Could anyone tell me
the reason behind this.

Thanks
ishekara

So that any and all instantiations ( objects ) of the empty version of
class A will have distinct addresses ( evidently at least one byte away
). Thus pointers, references et al of distinct objects will be different.
 
M

Mike Hewson

Shan said:
There is no requirement in the C++ standard that an empty object should
have one byte of memory aoocupied. It is purely based on the
implementation.

Sorry :)
but, not if it's conforming ( ISO/IEC 14882 p.149 ):

9 Classes [class]
..
..
..
3 Complete objects and member subobjects of class type shall have
nonzero size. ...
 
M

Mike Hewson

Shan said:
Sorry, i missed that....And thanks ;))

Actually I love the way English, as a common language, divides us. :)

If I say "class X is of size 10 bytes", does that imply that "one byte
of memory occupied". Well yes, it does, but I just didn't mention the
other nine.
 
O

owl ling

because a objeect's size can not be 0, it is a instance.
but sonetime,a class's size may be 0 when the class will haven't a
instance at anytime.

to read a words what was called "countting object in c++".by meyers.
you will get more things.
good luke
 
M

Mike Wahler

Shan said:
There is no requirement in the C++ standard that an empty object should
have one byte of memory aoocupied.

True, but it does require that it have a nonzero size.
It is purely based on the
implementation.

Yes, the exact nonzero size is implementation-defined.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top