C++,java 中空类的大小是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4789910/
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
what is the size of empty class in C++,java?
提问by garima
What is the size of an empty class in C++ and Java?
Why is it not zero?
sizeof();
returns 1 in the case of C++.
C++ 和 Java 中空类的大小是多少?为什么不是零?
sizeof();
在 C++ 的情况下返回 1。
回答by Martin York
Short Answer:
简答:
The standard explicitly says that a class can not have zero size.
该标准明确规定一个类的大小不能为零。
Long Answer:
长答案:
Because each object needs to have a unique address (also defined in the standard) you can't really have zero sized objects.
Imagine an array of zero sized objects. Because they have zero size they would all line up on the same address location. So it is easier to say that objects can not have zero size.
因为每个对象都需要有一个唯一的地址(也在标准中定义),所以您不能真正拥有零大小的对象。
想象一个大小为零的对象数组。因为它们的大小为零,所以它们都会排列在相同的地址位置。所以更容易说对象不能有零大小。
Even though an object has a non zero size, if it actually takes up zero room it does not need to increase the size of derived class:
即使一个对象的大小非零,如果它实际上占用了零空间,它也不需要增加派生类的大小:
Example:
例子:
#include <iostream>
class A {};
class B {};
class C: public A, B {};
int main()
{
std::cout << sizeof(A) << "\n";
std::cout << sizeof(B) << "\n";
std::cout << sizeof(C) << "\n"; // Result is not 3 as intuitively expected.
}
g++ ty.cpp
./a.out
1
1
1
回答by Stephen C
In the Java case:
在 Java 的情况下:
- There is no simpleway to find out how much memory an object occupies in Java; i.e. there is no
sizeof
operator. - There are a few ways (e.g. using
Instrumentation
or 3rd party libraries) that will give you a number, but the meaning is nuanced1; see In Java, what is the best way to determine the size of an object? - The size of an object (empty or non-empty) is platform specific.
- 在 Java 中没有简单的方法可以找出一个对象占用了多少内存;即没有
sizeof
运营商。 - 有几种方法(例如使用
Instrumentation
或 3rd 方库)会给你一个数字,但含义是细微的1;请参阅在 Java 中,确定对象大小的最佳方法是什么? - 对象的大小(空或非空)是平台特定的。
The size of an instance of an "empty class" (i.e. java.lang.Object
) is not zero because the instance has implicit state associated with it. For instance, state is needed:
“空类”(即java.lang.Object
)的实例的大小不为零,因为该实例具有与其关联的隐式状态。例如,需要状态:
- so that the object can function as a primitive lock,
- to represent its identity hashcode,
- to indicate if the object has been finalized,
- to refer to the object's runtime class,
- to hold the object's GC mark bits,
- and so on.
- 以便对象可以用作原始锁,
- 表示其身份哈希码,
- 指示对象是否已完成,
- 引用对象的运行时类,
- 保存对象的 GC 标记位,
- 等等。
Current Hotspot JVMs use clever tricks to represent the state in an object header that occupies two 32 bit words. (This expands in some circumstances; e.g. when a primitive lock is actually used, or after identityHashCode()
is called.)
当前的 Hotspot JVM 使用巧妙的技巧来表示占据两个 32 位字的对象头中的状态。(这在某些情况下会扩展;例如,当实际使用原始锁时,或在identityHashCode()
调用之后。)
1 - For example, does the size of the string object created by new String("hello")
include the size of that backing array that holds the characters? From the JVM perspective, that array is a separate object!
1 - 例如,创建的字符串对象new String("hello")
的大小是否包括保存字符的后备数组的大小?从 JVM 的角度来看,该数组是一个单独的对象!
回答by Mikael Persson
Because an object has to have an address in memory, and to have an address in memory, it has to occupy "some" memory. So, it is usually, in C++, the smallest possible amount, i.e. 1 char (but that might depend on the compiler). In Java, I wouldn't be so sure.. it might have some default data (more than just a placeholder like in C++), but it would be surprising if it was much more than in C++.
因为一个对象必须在内存中有一个地址,并且在内存中有一个地址,所以它必须占用“一些”内存。因此,在 C++ 中,它通常是最小的可能数量,即 1 个字符(但这可能取决于编译器)。在 Java 中,我不太确定……它可能有一些默认数据(不仅仅是像 C++ 中的占位符),但如果它比 C++ 中的多得多,那将是令人惊讶的。
回答by Jeremiah Willcock
Because every C++ object needs to have a separate address, it isn't possible to have a class with zero size (other than some special cases related to base classes). There is more information in C++: What is the size of an object of an empty class?.
因为每个 C++ 对象都需要有一个单独的地址,所以不可能有一个大小为零的类(除了一些与基类相关的特殊情况)。C++中有更多信息:空类的对象的大小是多少?.
回答by Jerry Coffin
C++ requires that a normal instantiation of it have a size of at least 1 (could be larger, though I don't know of a compiler that does that). It allows, however, an "empty base class optimization", so even though the class has a minimum size of 1, when it's used as a base class it does nothave to add anything to the size of the derived class.
C++ 要求它的正常实例化的大小至少为 1(可能更大,但我不知道有这样的编译器)。然而,它允许“空基类优化”,因此即使类的最小大小为 1,当它用作基类时,它也不必向派生类的大小添加任何内容。
I'd guessJava probably does pretty much the same. The reason C++ requires a size of at least 1 is that it requires each object to be unique. Consider, for example, an array of objects with size zero. All the objects would be at the same address, so you'd really only have one object. Allowing it to be zero sounds like a recipe for problems...
我猜Java 可能也差不多。C++ 要求大小至少为 1 的原因是它要求每个对象都是唯一的。例如,考虑一个大小为零的对象数组。所有对象都位于同一地址,因此您实际上只有一个对象。让它为零听起来像是解决问题的秘诀......
回答by Jon Purdy
It's defined by the C++ standard as "a nonzero value", because an allocated object must have a nonzero size in order to have a distinct address. A class that inherits from an empty class, however, is not required to increase in size, barring the usual increase of a vtable if there are virtual functions involved.
它由 C++ 标准定义为“非零值”,因为分配的对象必须具有非零大小才能具有不同的地址。但是,从空类继承的类不需要增加大小,除非涉及虚函数时通常增加 vtable。
回答by Manidip Sengupta
I don't know if there is a sizeof() operator in java. What you can do is create an instance of the empty class (have it serializable), send it through a PipedOutputStream and read it as byte array - byteArray.length gives you the size.
我不知道 java 中是否有 sizeof() 运算符。您可以做的是创建一个空类的实例(使其可序列化),通过 PipedOutputStream 发送它并将其作为字节数组读取 - byteArray.length 为您提供大小。
Alternatively, write out the instance to a file using DataOutputStream, close the File, open it and file.length() will give you the size of the Object. Hope this helps, - M.S.
或者,使用 DataOutputStream 将实例写入文件,关闭文件,打开它,file.length() 将为您提供对象的大小。希望这有帮助, - MS
回答by David Rodríguez - dribeas
As others have pointed out, C++ objects cannot have zero size. Classes can have zero size only when they act as a subclass of a different class. Take a look at @Martin York's answerfor a description with examples --and also look and vote the other answers that are correct to this respect.
正如其他人指出的那样,C++ 对象的大小不能为零。仅当类充当不同类的子类时,它们的大小才能为零。看一看@Martin York 的回答以获取示例的描述——并查看并投票其他在这方面正确的答案。
In Java, in the hotspot VM, there is a memory overhead of 2 machine-words (usually 4 bytes in a 32 arch per word) per object to hold book keeping information together with runtime type information. For arrays a third word is required to hold the size. Other implementations can take a different amount of memory (the classic Java VM, according to the same reference took 3 words per object)
在 Java 中,在热点 VM 中,每个对象有 2 个机器字(通常每个字 32 个字中的 4 个字节)的内存开销,用于保存簿记信息和运行时类型信息。对于数组,需要第三个字来保存大小。其他实现可以占用不同的内存量(经典的 Java VM,根据同一个引用,每个对象占用 3 个字)