0

I ran the following code.

#include <iostream>
using namespace std;

class Base
{  
    char c;

    public:

        virtual ~Base()
        {
        }
};

int main()
{   
  cout << sizeof(Base) << endl;
  return 0;
}

1) The size is 4 (for vptr) + 1(for char). But the result is 8. Why is it so ?

2) I replaced char with a int variable, still the output is 8. Can someone explain me what has caused this issue?

Sathish
  • 227
  • 3
  • 11

1 Answers1

3

It's down to padding. The compiler has packed your class to a multiple of 4 bytes.

Sean
  • 60,939
  • 11
  • 97
  • 136