Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: All
Oct 29, 1998

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.
Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date