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++.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- Sorting 3 numbers without branching [closed]
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
In the Java case:
sizeof
operator.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: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.)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? .