Size of sizeof for C ++ interviews

Source: Internet
Author: User

I have previously written the number of bytes for viewing the C ++ data type, but in practice, we are more concerned with the size of struct and class. First, let's look at the size of the empty class, as shown below:

class VoidClass{};

Run the program and it is found that the sizeof (voidclass) result is 1. This involves the concept of class instantiation. The so-called class instantiation is to allocate an address in the memory, each instance has a unique address in the memory. Similarly, the empty class will be instantiated, so the compiler will implicitly Add a byte to the empty class, so that the empty class has a unique address after instantiation, so the empty class's sizeof is 1.

Let's take a look at the situation where the class is not empty. When the class is not empty, the size of the sizeof mainly depends on two factors: the size and alignment of elements, and virtual functions. First, let's take a look at the alignment problem of C ++ (from Objective C ++): Many Computer Architectures require that a specific type be placed on a specific memory address. For example, it may require that the pointer address be a four-factor (four-byte aligned) or an eight-factor (eight-byte aligned ). If this constraint is not observed, hardware exceptions may occur during runtime. Some architectures are benevolent and less overbearing, but claim that they provide better efficiency if they meet the conditions of the world. Such as Intel
The double in the X86 architecture can be aligned with any byte boundary, but if it is an 8-byte homogeneous bit, its access speed will be much faster. Generally, 32-bit C ++ uses an 8-byte peer interface to increase the running speed. Therefore, the compiler tries its best to place data in the world to improve the memory hit rate. This field can be changed.

#pragma pack(x)

Macros can change the method of the compiler's peer interface. The default value is 8. C ++ is a smaller method than its own size. For example, if you specify that the compiler is bounded by 2 pairs and the size of the int type is 4, the int pair is bounded by 2 and 2, which is smaller than 4. When the default method is used, because almost all data types are not greater than the default method 8 (except long double ), therefore, all inherent types of peer methods can be considered as the size of the type itself. For composite data types, such as Union, struct, and class, the alignment is the alignment of members with the largest alignment among members. For the following two classes:

class ClassA{char a;char b;int c;double d;};class ClassB{char   a;double d;int    c;char   b;};

Run the program and find that the sizeof (classa) and sizeof (classb) results are 16 and 24 respectively. Its storage structure is

But do not consider its alignment as its size. Let's look at the following example:

class Sample1{private:char a[8];};class Sample2{private:double b;};class Sample3 : public Sample1{private:char c;};class Sample4 : public Sample2{private:char d;};

The running programs can find that their sizes are 8, 8, 9, and 16 in sequence. Although sample1 and sample2 are both 8 in size, sample1 is aligned with 1 and sample is 8, so this difference exists in sample3 and sample4.
General functions do not affect the class size, but virtual functions are not general functions. Let's take a look at the situation of virtual functions:

class BaseClass{public:BaseClass();virtual ~BaseClass();private:char p[8];};class DerivedClass : public BaseClass{private:char q;};

Running programs can find that their sizes are 12 and 16 in sequence. It can be seen that after a virtual function is added, the structure of the class has also changed. The size of the derivedclass is no longer 9 as sample3, but 16. This is because when there is a virtual function in the C ++ class, there is a pointer (vptr) pointing to the virtual function table, and the pointer size is allocated to 4 in the 32-bit system, then the method of peer interface becomes 4, so this result is returned.

Update 2012-10-27: Sizeof computing http://blog.csdn.net/qinwang_gz/article/details/8112991 containing bit Domains

Sizeof computing http://blog.csdn.net/hackbuteer1/article/details/7883531 containing virtual functions

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.