Why an empty Structure will occupy 2Bytes in C++

Showing Answers 1 - 5 of 5 Answers

sukhvir

  • Sep 26th, 2007
 

Size of empty structure in C++ is 1 byte, because compiler assign 1 byte for structure name.

  Was this answer useful?  Yes

mucdull

  • Nov 16th, 2008
 

Because the class has no members (especially no virtual functions) its objects naturally should have 0 bytes. However the objects must be legally creatable because the class is not abstract (having no pure virtual functions) so there must be a way to address the objects. You cannot address something that does not take up any memory. Since the smallest addressable space is 1 byte, the compiler allocates 1 byte for the objects so to make them addressable.

To dwell on the topic, what do you think is the size of objects of this class?

class C { virtual void Go() {} };

dkuy1

  • Aug 8th, 2009
 

The Standard C++ language definition says:
A class with an empty sequence of members and base class objects is an empty
class. Complete objects and member sub-objects of an empty class type shall have
nonzero size.
Most compiler give a size of 1.


cantrip . org/emptyopt . html This link explains in great detail why this is
and when it can have a different size if use as part of a class or struct:

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions