Search code examples
c++classunions

Why the size of class is 1 byte contaning union members?


I'm working on C++: Following is the code snippet:

class my
{
  union o
  {
    int i;
    unsigned int j;
  };

  union f
  {
    int a;
    unsigned int b;
  };
};

I found the size of class "my" is 1 byte. I don't understood why the value is 1 byte? Can any one explain me this result?


Solution

  • Your class is empty, it's contains two union type declarations but no data members. It's usual for empty classes to have a non-zero size, as explained here