devxlogo

An Object Size May Never Be Zero

An Object Size May Never Be Zero

An empty class doesn’t have any data members or member functions. You’d think that the size of an object of such a class would be zero. However, the Standard states that a complete object shall never have a zero value. For example:

 class Empty {};Empty  e; // e occupies at least 1 byte of memory

There is a good reason for this restriction. If an object were allowed to occupy 0 bytes of memory, its address could overlap with the address of a different object. The most obvious case would be an array of empty objects. To avoid this, an object occupies at least one byte of memory, which guarantees that it also has a distinct memory address.

devx-admin

Share the Post: