vija > I wanted to know why an empty class size is one byte bytes,
Vijay start
ost 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
ost 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