Empty class takes a byte?

A

avranju

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!
 
N

Neelesh Bodas

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

usr.root

(e-mail address 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.
 
F

Ferdi Smit

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 address removed)
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,177
Latest member
OrderGlucea
Top