top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C++: Why size of an empty class object is 1 byte ?

+4 votes
582 views
C++: Why size of an empty class object is 1 byte ?
posted Oct 18, 2013 by Neeraj Mishra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
May be its mandated by standard...

1 Answer

+1 vote

Consider the following cases case of empty classes contained in another.

class Empty
{ };

class TwoEmpties
{
  Empty a;
  Empty b;
};

You may want the addresses of the two members, &TwoEmpties::a and &TwoEmpties::b, to be different. For this to happen they must have size > 1. (or the compiler would have to add padding between them, which would in turn complicate the rules for when and where the compiler can add padding to classes.)

answer Oct 18, 2013 by Luv Kumar
...