468,514 Members | 1,669 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,514 developers. It's quick & easy.

why is size of object of empty class is 1 byte

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
Jul 22 '05 #1
8 2785

"ishekara" <is******@ishekara.com> wrote in message
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.


http://www.research.att.com/~bs/bs_f...l#sizeof-empty

Sharad
Jul 22 '05 #2
>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 membervariable then also the size of the object is 1 byte. Could anyone tell methe 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.

Jul 22 '05 #3
ishekara wrote:
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.

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #4
Shan wrote:
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.


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. ...

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #5
> 3 Complete objects and member subobjects of class type shall have
nonzero size. ...


Sorry, i missed that....And thanks ;))

Jul 22 '05 #6
Shan wrote:
3 Complete objects and member subobjects of class type shall have
nonzero size. ...

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.

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #7
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

Jul 22 '05 #8

"Shan" <sh*******@rediffmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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.


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
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jéjé | last post: by
9 posts views Thread by pembed2003 | last post: by
3 posts views Thread by spielmann | last post: by
7 posts views Thread by Martin Robins | last post: by
8 posts views Thread by Dave | last post: by
2 posts views Thread by Harish | last post: by
7 posts views Thread by Ajay | last post: by
30 posts views Thread by ggnguser | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.