Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Empty class takes a byte?

Reply
Thread Tools

Empty class takes a byte?

 
 
avranju@gmail.com
Guest
Posts: n/a
 
      11-18-2005
Hi,

We have a few exception marker classes in a project that are used only
to throw exceptions. I mean stuff like this (this may not be such a
hot idea for doing exception handling - a simple enum might suffice
since we aren't sending any data with the exception - but that's sort
of off-topic I suppose):

class Whoops{};
class GeeDontKnowHowToDoThat{};

Just out of curiosity I wanted to see if such classes actually consumed
any memory. The following program,

#include <iostream>

using namespace std;

class Foo{};

int main()
{
cout<<"sizeof( Foo ) = "<<sizeof( Foo )<<endl;
Foo *p = new Foo;
cout<<(int)*( (char *)p )<<endl;
delete p;
return 0;
}

produces the following output on MSVC++ 7.1:

sizeof( Foo ) = 1
120

So, looks like an empty class like this still consumes 1 byte of memory
and some random data is put there. Is that really the case?

Thanks!

--
Ranju. V
http://www.geocities.com/cool_ranju/
--

 
Reply With Quote
 
 
 
 
Neelesh Bodas
Guest
Posts: n/a
 
      11-18-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> So, looks like an empty class like this still consumes 1 byte of memory
> and some random data is put there. Is that really the case?
>


An empty class cannot have a zero size - what will be the address of
an instance of such a class? Observe that two different variables will
end up having the same memory address under such circumstances. Hence
size of empty class is usually 1.

 
Reply With Quote
 
 
 
 
usr.root@gmail.com
Guest
Posts: n/a
 
      11-18-2005

(E-Mail Removed) 写道:

> Hi,
>
> We have a few exception marker classes in a project that are used only
> to throw exceptions. I mean stuff like this (this may not be such a
> hot idea for doing exception handling - a simple enum might suffice
> since we aren't sending any data with the exception - but that's sort
> of off-topic I suppose):
>
> class Whoops{};
> class GeeDontKnowHowToDoThat{};
>
> Just out of curiosity I wanted to see if such classes actually consumed
> any memory. The following program,
>
> #include <iostream>
>
> using namespace std;
>
> class Foo{};
>
> int main()
> {
> cout<<"sizeof( Foo ) = "<<sizeof( Foo )<<endl;
> Foo *p = new Foo;
> cout<<(int)*( (char *)p )<<endl;
> delete p;
> return 0;
> }
>
> produces the following output on MSVC++ 7.1:
>
> sizeof( Foo ) = 1
> 120
>
> So, looks like an empty class like this still consumes 1 byte of memory
> and some random data is put there. Is that really the case?


if you define two data like this :
Foo *p = new Foo;
Foo *p1 = new Foo;
you want the p and p1not to be the same,so the c++'s father put 1 byte
of memory in each empty class.
and your code in this line: cout<<(int)*( (char *)p )<<endl;
really needs to write like this :cout<<*( (char *)p )<<endl; to prove
you case.

 
Reply With Quote
 
Ferdi Smit
Guest
Posts: n/a
 
      11-18-2005
(E-Mail Removed) wrote:
> Hi,
>


> So, looks like an empty class like this still consumes 1 byte of memory
> and some random data is put there. Is that really the case?
>


Yes:

"A class with an empty sequence of members and base class objects is an
empty class. Complete objects and member subobjects of an empty class
type shall have nonzero size. 1)

1) That is, a base class subobject of an empty class type may have zero
size."

So there is one allowed optimization when the empty class is a base
class. This is known as the empty-base class optimization. Note that a
compiler is not required to implement this.

A nice page describing this feature and some of its uses can be found
here: http://www.cantrip.org/emptyopt.html

--
Regards,

Ferdi Smit (M.Sc.)
Email: (E-Mail Removed)
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
 
Reply With Quote
 
Ranju. V
Guest
Posts: n/a
 
      11-18-2005
Alrighty folks!

Thanks for your time!

--
Ranju. V
http://www.geocities.com/cool_ranju/
--

 
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
Altova Mapforce - xml 2 xml map: empty elements output although input element is not empty Lukas XML 3 11-10-2005 02:25 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
empty lists vs empty generators Brian Roberts Python 12 05-04-2005 08:59 PM
Check if a directory is empty and empty it Marcia Hon C Programming 8 02-14-2004 03:53 AM
empty/non-empty element John XML 1 07-16-2003 10:23 AM



Advertisments