0

Normally, if a class has other non-static members, the size of static variables do not count towards the size of the class (or struct). For example,

struct A
{
   float f1;
   float f2;
   double d1;
};

This struct would be 4 + 4 + 8 = 16 bytes (on an x86 PC).

struct A
{
   float f1;
   float f2;
   static double d1;
};

Making the double d1 static would reduce it to 8 bytes.

However if I make all three members static, the size comes out to be 1. Why is that? In fact, removing all the variables has the same effect.

Someone told me it was because the object needs to keep track of its address, but an address is 4 bytes long isn't it? What's more, why isn't this 1 byte added to the size when I have non-static member variables? What is the purpose of this one byte if there are no non-static member variables or virtual functions?

The Unknown Dev
  • 3,039
  • 4
  • 27
  • 39

0 Answers0