473,657 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 GeeDontKnowHowT oDoThat{};

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 2570
av*****@gmail.c om 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.c om 写道:
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 GeeDontKnowHowT oDoThat{};

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.c om 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
8886
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 a empty value instead of returning the browser type. Here is the line which i am using in my code and from manual: <?php echo $_SERVER; ?>
2
1846
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 socketbuffer is empty. This connection is being used to transmit data following a RFC so I cann't create a control protocol that tells me the data was received. (And even if I could, I want the timeout procedure to start after I send all data, because that...
4
4939
by: Sweety | last post by:
hello, why sizeof an empty class why 1 byte ? plz illustrate this ? bye
8
3084
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 is 1 byte. Could anyone tell me the reason behind this. Thanks ishekara
6
4877
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
1797
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(); Console.WriteLine(count++ + " '" + d + "' --> " + d.Length); Sometimes "d" should be empty, and sometimes it's a date string. And in the VS.NET watch window, it's indeed empty. Except that my diagnostic line sometimes prints just the number...
1
6597
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 Byte() {} (i.e. Byte array with zero elements) in a row of an instance of the class prior to serialization using the binary formatter. When I attempt to DeSerialize the instance, the following exception is thrown: :...
7
4473
by: Ajay | last post by:
Hi all, Please tell me why empty class has one Byte Size. thanks in Advance.. Ajay
9
4086
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 each class takes up at least 200 bytes". The 200 bytes are byte-code, not size of a class instance. Since Java and C# is so similar, I am wandering how many bytes will it take for a C# class after compiled into IL code. If it also consumes
0
8319
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8837
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7347
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2739
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.