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