0

Assuming the following code:

#include <iostream>
#include <stdint.h>

class FooBool
{ bool a; };

class FooUint16
{ uint16_t a; };

class FooBoth
{ bool a; uint16_t b;};

class FooThree
{ bool a; uint16_t b; uint16_t c; };

int main()
{
    // Expected 1; Result: 1
    { bool a;      std::cout << "bool:      " << sizeof(a) << std::endl; }
    // Expected 2; Result: 2
    { uint16_t a;  std::cout << "uint16_t:  " << sizeof(a) << std::endl; }
    // Expected 1; Result: 1
    { FooBool a;   std::cout << "FooBool:   " << sizeof(a) << std::endl; }
    // Expected 2; Result: 2
    { FooUint16 a; std::cout << "FooUint16: " << sizeof(a) << std::endl; }
    // Expected 3 (1 + 2); Result: 4
    { FooBoth a;   std::cout << "FooBoth:   " << sizeof(a) << std::endl; }
    // Expected 5 (1 + 2 + 2); Result: 6
    { FooThree a;  std::cout << "FooThree:  " << sizeof(a) << std::endl; }

    return 0;
}

Where does the extra byte for FooBoth and FooThree come from?

It's not using any virtual classes, is it? Is there an overhead per variable? If so, why is there only one in FooThree?

Johan de Vries
  • 175
  • 1
  • 9

0 Answers0