Consider a class like the one above. Commonly called an "empty class," this class has no internal variables. So, how big is this empty class?
At first glance, the size should be 0 since there are no member variables. However, the size is never 0 in any language, whether Java, C#, C (in this case, a struct), or C++. This is to ensure that two different objects never have the same address.
Empty classes typically have a size of 1 byte in a 32-bit environment and 2 bytes in a 64-bit environment. However, the exact size cannot be determined. According to the specification, the size just needs to be non-zero. The precise size depends on the implementation.
This is a translation of my old Korean post written in 2015.
Because the size can vary depending on the implementation, it is now possible to have different sizes (although still not 0). And Languages like Rust have even introduced zero-sized types. We will look at this topic in more detail at a future opportunity.
Comments
Post a Comment