1#include <iostream>2 3 #pragmaPack (4)4 5 structa{6 A () {}7~A () {}8 intM1;//4 bytes9 intm2; 4 bytesTen }; One structb:a{ A B () {} -~B () {} - intM1;//4 bytes the Charm2; - Static Charm3; - }; - structc{ + C () {} - Virtual~C () {}//64-bit system is 8 bytes//32-bit system is 4 bytes + intM1; 4 bytes A Shortm2; at }; - - intMain () - { -Std::cout <<sizeof(A) <<Std::endl; -Std::cout <<sizeof(B) <<Std::endl; inStd::cout <<sizeof(C) <<Std::endl; -Std::cout <<sizeof(int*) <<Std::endl; to + return 0; -}
sizeof (A) = 4 + 4 = 8 byte
sizeof (B) = sizeof (A) + 4 + 1 (requires memory alignment) = Byte
sizeof (C) = virtual function pointer size 4 + 4 + 2 (requires memory alignment) = Byte
sizeof (A) = 4 + 4 = 8 byte
sizeof (B) = sizeof (A) + 4 + 1 (requires memory alignment) = Byte
sizeof (C) = virtual function pointer size 8 + 4 + 2 (requires memory alignment) = Byte
C + + Note memory alignment