开发者

sizeof(blank class) == 1. why? [duplicate]

This question already has answers here: 开发者_如何学编程 Closed 12 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜