C + + sizeof

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.