top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

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

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

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
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...