sizeof(blank class) == 1. why? [duplicate]
Possible Duplicate:
C++: What is the size of an object of an empty class?
#include <iostream>
class C
{
};
int main()
{
std::cout << sizeof(C) << std::endl;
return 0;
}
output: 1
Why 1, but not zero ?
From Stroustrup's mouth sizeof. To ensure that the addresses of two different objects will be different. For the same reason, "new" always returns pointers to distinct objects.
Because the C++ standard requires all objects to have a nonzero size. This helps ensure that every object has a unique address.
The c++ standard says that every class/struct must have at least 1 byte.
精彩评论