What is the size of the class?

Possible duplicate:
Empty class in C ++

class Class1 { char c; }; class Class2 { }; 

What are the sizes of Class1 and Class2?

In VC6, I got both: 1. Can someone explain this?

+4
source share
1 answer

No class can be smaller than one, because pointer arithmetic (in particular, the subtraction operator) can be divided by size, and division by zero can be undefined. It is also necessary that each instance has a unique address, which means that each of them must be assigned at least one byte of address space, so the minimum size is again one.

So sizeof (Class1) == 1 , because it is necessary for the content, and sizeof (Class2) == 1 , because it is the minimum allowable.

+6
source

All Articles