By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,083 Members | 2,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,083 IT Pros & Developers. It's quick & easy.

Empty class takes a byte?

P: n/a
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/
--

Nov 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
av*****@gmail.com 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.

Nov 22 '05 #2

P: n/a

av*****@gmail.com 写道:
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.

Nov 22 '05 #3

P: n/a
av*****@gmail.com 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: Fe********@cwi.nl
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
Nov 22 '05 #4

P: n/a
Alrighty folks!

Thanks for your time!

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

Nov 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.