Re: Why empty class size is 1 Byte ???

J

John Carson

vijay said:
vijay > Thanks, But this again raises an other doubt >But when u
create a integer the size of class then would then be 4 and not 5
bytes,

You mean a class the size of an integer? On my computer:

class X
{
};

class Y
{
int a;
};

int main()
{
std::cout << sizeof(X) << '\n';
std::cout << sizeof(Y) << '\n';
}

produces an output of 1 and 4.
 
V

vijay

vija > I wanted to know why an empty class size is one byte
carson >Perhaps so each instance of the class has a distinct address.

class X
{
};

class Y
{
int a;
};

int main()
{
std::cout << sizeof(X) << '\n';
std::cout << sizeof(Y) << '\n';
}

produces an output of 1 and 4.

Vijay start :post 3
Hello
Yes , exactly, I want to know why the size of y is not 5 bytes, since int -
4 bytes is an addition over 1 byte ?
Vijay end :post 3
 
J

John Carson

vijay said:
vija > I wanted to know why an empty class size is one byte


Vijay start :post 3
Hello
Yes , exactly, I want to know why the size of y is not 5 bytes, since
int - 4 bytes is an addition over 1 byte ?
Vijay end :post 3


Because compilers are not bound by any rule that says that class size must
increase by the size of what is added. If you add an extra byte to an empty
class so each object has a distinct address, then you clearly no longer need
the extra byte when the class is non-empty. So the compiler discards it (or,
more accurately, doesn't add it in the first place).
 
G

Gianni Mariani

vijay said:
Vijay start :post 3
Hello
Yes , exactly, I want to know why the size of y is not 5 bytes, since int -
4 bytes is an addition over 1 byte ?

Because it's an "optimization". Inheriting zero length classes does not
increse the size of the class it is inheriting. However, to instantiate
a zero length class will mean that the minimum size of 1 byte needs to
be allocates so that an array may have distinct addresses.

Early compilers had an overhead and would have had a size implication on
inherited classes. The latest compilers do this optimization so that
you can inherit as many zero length classes as you want and have no
impact on memory utilization in the final class.

How would you like it to work ?

G
 
J

John Harrison

Gianni Mariani said:
Because it's an "optimization". Inheriting zero length classes does not
increse the size of the class it is inheriting. However, to instantiate
a zero length class will mean that the minimum size of 1 byte needs to
be allocates so that an array may have distinct addresses.

Early compilers had an overhead and would have had a size implication on
inherited classes. The latest compilers do this optimization so that
you can inherit as many zero length classes as you want and have no
impact on memory utilization in the final class.

How would you like it to work ?

G

There isn't any inheritance in John Carson's example, which makes Vijay's
objections even harder to understand.

john
 
A

Alexander Terekhov

Gianni Mariani wrote:
[...]
Because it's an "optimization". Inheriting zero length classes does not
increse the size of the class it is inheriting. However, to instantiate
a zero length class ....

There's no such thing as "zero length class" in C++. Sizeof is always
positive in C++ (that's not the case in C99, BTW).

regards,
alexander.
 
J

Jakob Bieling

vija > I wanted to know why an empty class size is one byte bytes,


Vijay start :post 3
Hello
Yes , exactly, I want to know why the size of y is not 5 bytes, since nt -
4 bytes is an addition over 1 byte ?
Vijay end :post 3


Try to think of it this way: class Y above is as large as the 'int' it
contains, which is 4 bytes. Class X is also as large as the data member it
contains, and since it contains no data members, that is 0 bytes. But
because a class/struct cannot be 0 bytes, because each instance of the
class/struct must have its own unique address (as JohnCarsonalready told
you), the compiler just adds a byte for padding.
But that is not the only case where the compiler adds additional bytes
to your class. For the following class, most compiler add a padding byte:

class Z
{
int a,
char b[3];
};

sizeo (Z) is 8 on a lot compilers, even though you might expect it to be
7.

hth
 
G

Gianni Mariani

Alexander said:
Gianni Mariani wrote:
[...]
Because it's an "optimization". Inheriting zero length classes does not
increse the size of the class it is inheriting. However, to instantiate
a zero length class ....


There's no such thing as "zero length class" in C++. Sizeof is always
positive in C++ (that's not the case in C99, BTW).

regards,
alexander.



#include <iostream>

using namespace std;

struct A
{
};

struct B : A
{
int a;
};

int main()
{

cout << "sizeof(A) : " << sizeof(A) << endl;
cout << "sizeof(B) : " << sizeof(B) << endl;
}

$ ./class
sizeof(A) : 1
sizeof(B) : 4


When A is inherited into B, A is contributing nothing to the size of B.

Hence, A is a "zero length" class in this case (when inherited in B).

Did I imagine somthing ?
 
A

Alexander Terekhov

Gianni Mariani wrote:
[...]
When A is inherited into B, A is contributing nothing to the size of B.

Hence, A is a "zero length" class in this case (when inherited in B).

Did I imagine somthing ?

Sort of. A is an empty class. An empty base class sub-objects may not
occupy storage -- that's an optimization known as EBO.

regards,
alexander.
 
Joined
Sep 23, 2008
Messages
1
Reaction score
0
Class size

It is just because an empty class is initialized by NULL. and the NULL is nothing but a char type , i.e size of 1.

When you declare some datatype in it, the class is not longer empty (NULL) type, so with an integer type size of class is 4 and not 5.



Rajesh Kumar
Gameshastra Soln
 
Joined
Apr 18, 2010
Messages
1
Reaction score
0
Size of an empty class.

Its one byte, also don't forget to read about Empty base optimization..

You can listen to voice notes on empty c++ class at listenvoice.com
 

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,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top