Click to See Complete Forum and Search --> : Class X {} sizeof(X) // = 1 ?


arunkumar_gona
July 12th, 2002, 01:30 PM
Class X
{

}

Class Y
{
char c;
}

class Z
{
int i;
}

Now:

sizeof(X) // == 1
sizeof(Y) // == 1
sizeof(Z) // == 4

Why is the size of class X is 1 when it is empty ?
I understand that sizeof(Y) & sizeof(Z) to be 1 and 4 because of the member variables.

Can someone enlighten me on this.

Graham
July 12th, 2002, 02:02 PM
Because no object/type can have zero size (it's in the standard, I think).

However:

class X {};

class Y : public X
{
char c;
};

// sizeof(Y) == 1, so it can contribute zero size to another object/type

arunkumar_gona
July 12th, 2002, 03:36 PM
Where can I find the C++ Standard , any site or any book.


Thanks.

Philip Nicoletti
July 12th, 2002, 04:33 PM
http://www.techstreet.com/ncitsgate.html

$18 for the PDF version


From Secion 1.8 : The C++ Memory Model ...

2) Objects can contain other objects, called sub-objects.
A sub-object can be a member sub-object (9.2), a base class
sub-object (clause 10), or an array element. An object that is
not a sub-object of any other object is called a complete object.

[from this class X is a complete object]


4) If a complete object, a data member (9.2), or an array element
is of class type, its type is considered the most derived class,
to distinguish it from the class type of any base class subobject;
an object of a most derived class type is called a most derived object.

[from this class X is a most derived object]

5) Unless it is a bit-field (9.6), a most derived object shall have
a non-zero size and shall occupy one or more bytes of storage. Base
class sub-objects may have zero size. An object of POD 4) type (3.9)
shall occupy contiguous bytes of storage.

[so Class X must occupy at least 1 byte of storage]

Anthony Mai
July 12th, 2002, 04:38 PM
Some where in the C++ standard it is specified that when a class is empty, its size is defined as 1 rather than zero. The reason for that is facilitate object counting in object arrays.

Why? The number of objects in an array generally can be calculated by take the offset (difference) between the point which points to just one element past the last one, minus the pointer to the first element, and then be divided by the size of the object. Obviously if the object size is zero, you can not divide by zero.

In my opinion such specification in the C++ standard is meaningless. If an object has no none-static member, then there is no difference between one instance of it and another instance. Henceful it is meaningless to have an array of such object. So, the need for a none-zero object size NEVER occurs.

Graham
July 15th, 2002, 04:20 AM
#include <iostream>
using namespace std;

struct X
{
void f()
{
cout << "Hello World" << endl;
}
};

struct Y
{
X x_;
int i_;
};

struct Z : X
{
int i_;
};

int main()
{
cout << "sizeof(X) = " << sizeof(X) << endl;
cout << "sizeof(Y) = " << sizeof(Y) << endl;
cout << "sizeof(Z) = " << sizeof(Z) << endl;
return 0;
}

The above code generates the output:

sizeof(X) = 1
sizeof(Y) = 8
sizeof(Z) = 4

This generates the same output if I make X a completely empty structure. So, X with member functions but no data is a (potentially) useful class of zero size.

Now, consider this (given the above declarations):

X ax[10];
int n = sizeof(ax)/sizeof(ax[0]);


what's the value of n, if sizeof(ax[0]) returns 0?

Graham
July 15th, 2002, 04:24 AM
The point being that, when you're writing a standard, you can't just cater for what what might be sensible. You have to make allowance for all the dumb things people might (and probably will) do. Since an array of empty structures is a possibility, and further, someone, somewhere, will try to divide by the size of that structure (irrespective of how pointless that may be) the standard has to cater for it.

arunkumar_gona
July 15th, 2002, 10:55 AM
Thanks to all for replying to the post. The information provided was very useful.

amag
July 22nd, 2002, 10:45 AM
The reason is that a zero-sized object can't have an address, so all objects must have size > 0 (and thus an address).