C + + Various types of sizeof size, first put test code and test results (VS2013).
#include <iostream>using namespace STD;//////////Test base type//////////intGetarraysize (CharStr[]) {return sizeof(str);}voidTestsizeofbase () {cout<<"//////////test base type//////////"<< Endl;Char* PStr ="Hello World";CharArrstr[] ="Hello World";cout<<"Char:"<<sizeof(Char) << Endl;cout<<"Short:"<<sizeof( Short) << Endl;cout<<"int:"<<sizeof(int) << Endl;cout<<"float:"<<sizeof(float) << Endl;cout<<"Double:"<<sizeof(Double) << Endl;cout<<"char*:"<<sizeof(PSTR) << Endl;cout<<"string:"<<sizeof("Hello World") << Endl;cout<<"char[]:"<<sizeof(ARRSTR) << Endl;cout<<"char[] as param:"<<sizeof(Getarraysize (ARRSTR)) << Endl;cout<< Endl;}////////////Test Empty class////////////classemptya{};classEMPTYB: Publicemptya{};structemptystruct{};voidTestsizeofempty () {cout<<"////////////Test empty class////////////"<< Endl;cout<<"Emptya:"<<sizeof(Emptya) << Endl;cout<<"Emptyb:"<<sizeof(EMPTYB) << Endl;cout<<"Emptystruct:"<<sizeof(emptystruct) << Endl;cout<< Endl;}//////////Test Memory alignment//////////classaligna{CharA;};classalignb{CharA Shortb;};classalignc{CharA ShortbDoublec;};#pragma pack (1)classalignd{CharA ShortbDoublec;};#pragma pack ()voidTestsizeofalign () {cout<<"//////////Test memory alignment//////////"<< Endl;cout<<"Aligna:"<<sizeof(Aligna) << Endl;cout<<"ALIGNB:"<<sizeof(ALIGNB) << Endl;cout<<"ALIGNC:"<<sizeof(ALIGNC) << Endl;cout<<"ALIGND:"<<sizeof(ALIGND) << Endl;cout<< Endl;}////////////testing polymorphic////////////classvirtuala{Virtual voidTest ();};classvirtualb{Virtual voidTest ();Virtual voidTest2 ();};voidVirtualb::test () {}voidVirtualb::test2 () {}classvirtualc{CharAVirtual voidTest ();Virtual voidTest2 ();};voidTestsizeofvirtual () {cout<<"////////////test polymorphic////////////"<< Endl; Virtualb OBJB;cout<<"Virtuala:"<<sizeof(Virtuala) << Endl;cout<<"Virtualb:"<<sizeof(VIRTUALB) << Endl;cout<<"Virtualb Object:"<<sizeof(OBJB) << Endl;cout<<"Virtualc:"<<sizeof(Virtualc) << Endl;cout<< Endl;}voidTestsizeof () {testsizeofbase (); Testsizeofempty (); Testsizeofalign (); Testsizeofvirtual ();}intMainintargcChar**ARGV) {testsizeof (); GetChar ();return 0;}
Output results
Brief description
sizeof (Getarraysize (ARRSTR)) 4, because when a parameter is passed as a formal parameter, the array is converted to a pointer.
sizeof (empty Class) 1, because instantiating a class requires the compiler to allocate memory space to it, cannot be allocated to a size of 0 of memory, so the compiler allocates a byte by default to mark the class instance that might initialize, while making the empty class occupy the least amount of space (that is, 1 bytes).
Memory Alignment, the first address of an object can be divisible by the size of its widest base type member, and the offset (offset) of each member relative to the first address of the struct is an integer multiple of the member size, and if necessary the compiler adds padding bytes between members (internal adding) ; The total size is an integer multiple of the size of the widest base type member.
virtual function , virtual function table pointer, size equivalent to sizeof (void *).
C + + sizeof