The size of the instantiation object of the C + + class sizeof ()

Source: Internet
Author: User

The reason to write this "C + + class instantiation of the size of the sizeof ()", because in the written test encountered the following such a problem, then feel is this a hole, but, I still do not hesitate to jump down, because there is a blind spot of knowledge. Now, to summarize, you do not know the size of the instantiation object of the C + + class of sizeof ().

Class D{public:d () {}virtual ~d () {}private:int A; char *p;};

Example one:

Class a{}; A a;cout << sizeof (a) << Endl;
Run Result: 1

Explanation: Empty class, no member variable or function, that is, there is no storage content, but by a a, empty class can still be instantiated. A class can be instantiated, and the compiler will need to allocate memory space to indicate the address of the class instance . Here the compiler allocates a byte by default to mark a class instance that might initialize, while leaving the empty class with the least amount of space (that is, 1 bytes).

Example two:

Class b{private:int A;}; b b;cout << sizeof (b) << Endl;
Run Result: 4

Explanation: When there are other members in the class occupying space, that one byte is not counted, as in the case: The result is 4, not 1+4=5.

Example three:

Class bb{private:int A; char b;}; BB bb;cout << sizeof (BB) << Endl;
Run Result: 8

Explanation: What? How could it be 8? Shouldn't it be 4 + 1 = 5? This examines the alignment, which involves compiler optimizations . For most CPUs, the integer multiples of CPU length are faster, so for these members to add up to this integer multiple, it is possible that the compiler will insert superfluous content to fill this integer multiple, and sometimes adjacent members may also be inserted into the blank for this purpose, which is called "fill-in" (padding). Therefore, the C + + standard tightly stipulates that members are arranged in the order defined by the class, but are not required to be tightly aligned in memory. As a result, a char on the previous byte is fully stored and becomes 4 bytes.

Example four:

Class c{private:int A; char *p;}; C c;cout << sizeof (c) << Endl;
Run Result: 8

Explanation: In general, if it is a pointer, it accounts for 4 bytes of storage regardless of what data type the pointer is pointing to.

Example five:

Class D{public:d () {}virtual ~d () {}private:int A; char *p;};D d;cout << sizeof (D) << Endl;
Run Result: 12

Explanation: Investigate virtual functions. When a class contains virtual functions (whether its own virtual function or inherited), then there is a member variable information in the class: a virtual function pointer (4 bytes), which points to a virtual function table, the first item of the virtual function table is the TypeInfo information of the class, and then the entry is the address of all the virtual functions of the class.

A further explanation: when there are virtual functions in a class, the compiler inserts a table for the class that we don't see as loving you. This table is the virtual function table, and the data we can't see is the pointer to the virtual function table-the virtual table pointer . The virtual function table is to hold the address of the virtual function in the class. We can understand the virtual function table as an array, and each element in the array holds the address of the virtual function in the class. When the virtual function is called, the program does not jump directly to the code of the function as the normal function, but first takes out the virtual table pointer to get the address of the virtual function table, according to this to the virtual function list, from the table to remove the function pointer, and finally call the function.

Example SIX:

Class E{public:e () {}virtual ~e () {}private:int A; char *p;static int b;}; e e;cout << sizeof (e) << Endl;
Run Result: 12

Explanation: Examine memory allocations for static member variables. because a static member variable allocates space in a static store, it is not part of the instance, so static member variables in the class do not occupy space .

Example Seven:

Class F:public E{public:f () {}~f () {}private:int C;}; e e;cout << sizeof (e) << Endl;
Run Result: 16

Explanation: storage space for derived class objects = space for base class storage + non-static data members unique to derived classes

Example eight:

Class G:public Virtual E{public:g () {}~g () {}private:int C;}; G G;cout << sizeof (g) << Endl;
Run Result: 20

Explanation: If it is a virtual inheritance, the storage space size of the class object = storage space of the base class + the storage space of non-static data members that are unique to the derived class + the virtual function storage space for each class (this is additional, that is, by this formula, sizeof (g) = 12 ( E base class storage space) + 4 (G-specific storage space for non-static data members) + 4 (storage space for virtual functions of class E, if there are multiple virtual functions in the E class, only one time)).

Example nine:

Class H:public Virtual E{public:h () {}~h () {}virtual void GetValue () {}private:int C;}; H h;cout << sizeof (h) << Endl;
Run Result: 24

Explanation: compare example eight, as explained above: the storage space size of the class object = storage space of the base class + storage space for non-static data members unique to derived classes + virtual function storage space for each class (sizeof (h) = (e base class storage) + 4 ( G-specific storage space for non-static data members) + 4 (the storage space for virtual functions of class E, if there are multiple virtual functions in class E, only one time) + 4 (storage space for virtual functions of class H, if there are multiple virtual functions in the H class, only one time)).

As above, that is my summary of this type, this problem can only appear once!!!

Come on, put your mind on the plane. dwell on, must have echoes!!!

The size of the instantiation object of the C + + class sizeof ()

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.