top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

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

+4 votes
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

0 votes

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
Contact Us
+91 9880187415
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Karnataka INDIA.