为什么 C++ 中空类的大小不为零?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2362097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is the size of an empty class in C++ not zero?
提问by shreyasva
Possible Duplicate:
C++: What is the size of an object of an empty class?
可能的重复:
C++:空类的对象的大小是多少?
Why does the following output 1
?
为什么会出现以下输出1
?
#include <iostream>
class Test
{
};
int main()
{
std::cout << sizeof(Test);
return 0;
}
回答by Péter T?r?k
The standard does not allow objects (and classes thereof) of size 0, since that would make it possible for two distinct objects to have the same memory address. That's why even empty classes must have a size of (at least) 1.
该标准不允许大小为 0 的对象(及其类),因为这将使两个不同的对象具有相同的内存地址成为可能。这就是为什么即使是空类也必须具有(至少)1 的大小。
回答by Maurits Rijk
To ensure that the addresses of two different objects will be different. For the same reason, "new" always returns pointers to distinct objects.
保证两个不同对象的地址会不同。出于同样的原因,“new”总是返回指向不同对象的指针。
See Stroustrupfor complete answer.
有关完整答案,请参阅Stroustrup。
回答by wilhelmtell
The C++ standard guarantees that the size of any class is at least one. The C++ standard states that no object shall have the same memory address as another object. There are several good reasons for this.
C++ 标准保证任何类的大小至少为 1。C++ 标准规定任何对象都不应与另一个对象具有相同的内存地址。这有几个很好的理由。
To guarantee that
new
will always return a pointer to a distinct memory address.To avoid some divisions by zero. For instance, pointer arithmetics (many of which done automatically by the compiler) involve dividing by
sizeof(T)
.
为了保证
new
将始终返回一个指向不同内存地址的指针。避免一些除以零。例如,指针算术(其中许多由编译器自动完成)涉及除以
sizeof(T)
。
Note however that it doesn't mean that an empty base-class will add 1 to the size of a derived class:
但是请注意,这并不意味着空基类会将派生类的大小加 1:
struct Empty { };
struct Optimized : public Empty {
char c;
};
// sizeof(Optimized) == 1 with g++ 4.0.1
回答by Shantanu
Class without any data members and member function such type of class is known as empty class. Size of object of empty class is always 1 byte.
没有任何数据成员的类和成员函数这种类型的类称为空类。空类对象的大小始终为 1 个字节。
When we create object of any class at that time object always gets 3 characteristics i.e.
当我们创建任何类的对象时,对象总是会获得 3 个特征,即
- State
- Behaviour
- Identity
- 状态
- 行为
- 身份
When we create object of empty class at that time State of that object is nothing. Behaviour of that object is also nothing, but compiler assigns a unique address to that object. Memory in Computer is always organized in the form of bytes and minimum memory available at object address location is 1 byte. That's why size of object of empty class is 1 byte.
当我们在那个时候创建空类的对象时,那个对象的状态是什么。该对象的行为也没什么,但编译器为该对象分配了一个唯一的地址。计算机中的内存始终以字节的形式组织,对象地址位置的最小可用内存为 1 个字节。这就是为什么空类的对象大小为 1 字节的原因。
回答by Sebastian Mach
What Maurits and Péter said.
莫里茨和彼得所说的话。
It is interesting to note in this context that compilers can do empty base class optimization (EBCO):
有趣的是,在这种情况下,编译器可以进行空基类优化 (EBCO):
#include <iostream>
struct Foo {};
struct Bar : Foo {};
int main () {
std::cout << sizeof(Foo) << ',' << sizeof(Bar) << std::endl;
}
This will probably print "1,1" if you compile and run it. See also Vandevoorde/Josuttis 16.2on EBCO.
如果您编译并运行它,这可能会打印“1,1”。另见EBCO 上的Vandevoorde/Josuttis 16.2。