473,379 Members | 1,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Empty class takes a byte?

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
4 2546
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

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

Thanks for your time!

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

Nov 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns...
2
by: Robert A. van Ginkel | last post by:
Hello Fellow Developer, This looks like a long mail, but at the end of this post is my socket wrapper attached. I want to make a timeout procedure that starts counting down after the...
4
by: Sweety | last post by:
hello, why sizeof an empty class why 1 byte ? plz illustrate this ? bye
8
by: ishekara | last post by:
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...
6
by: free2cric | last post by:
Hi, What will the syntax sound like for checking size of an empty class? what will be the size. Thanks, Cric
7
by: Clinton Pierce | last post by:
I'm calling a method in another assembly that's supposed to return a string, and possibly that string might be empty. The code looks something like this: string d = r.Field("date").ToString();...
1
by: Taiwo | last post by:
I generated a Typed Dataset class including a base64Binary column. This column was specified as a .NET type of Byte() in the class that was auto-generated. I set the value of this property to New...
7
by: Ajay | last post by:
Hi all, Please tell me why empty class has one Byte Size. thanks in Advance.. Ajay
9
by: Morgan Cheng | last post by:
Hi, I once worked for java in embedded system. Since the memory is limited in embedded system, there are some guidelines for programming. One of them is "Don't create too many classes, because...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.