Memory layout of C ++ objects (I)

Source: Internet
Author: User
This document is excerpted from Pan Kai's "exploration of c ++ object layout and polymorphism implementation" series. For more information, see http://lunatic.bokee.com/1. Empty class: Class c000 {}; // sizeof = 1 its size is 1 byte, which is a placeholder and we can see that its value is 0xcc. In debug mode, this indicates that the memory is initialized by the debugging code inserted by the compiler. In release mode, it may be a random value. This byte ensures that c000 class instance objects have different memory addresses. &! = & B.

2. class c001 {int A ;}; // sizeof = 4 (unsigned) Char occupies 1 byte, pointer 4 byte, (unsigned) short2 byte, (unsigned) int 4 byte, (unsigned) long4 byte, float4 byte, double8 byte. Static member variables are stored in the global data zone and do not occupy the position in the stack. Therefore, they are not included in sizeof.

Class c002 {char c1 [2]; // sizeof = 1 char c2 [2]; // sizeof = 1 int I [2]; // sizeof = 4 }; // sizeof = 12 class c003 {char c1 [2]; // sizeof = 1 int I [2]; // sizeof = 4 char c2 [2]; // sizeof = 1}; // sizeof = 16 class c004 {double D; // sizeof = 8 int I; // sizeof = 4 }; // sizeof = 16 class c005 {char C [11]; // sizeof = 1 Double D; // sizeof = 8 }; // sizeof = 24 pay attention to the Declaration Order of the member variables in the class, that is, the order in which the variables are stored in the memory. Memory alignment is used here. The general principle of CPU optimization rules is as follows: for n-byte elements (n = 2, 4, 8 ...), The first address can be divisible by N to obtain the best performance. You can follow this principle when designing the compiler: For each variable, you can find the first address that meets this condition from the current position as the first address. It should be noted that even if this principle is adopted, the result of c002 should also be 13. However, structs generally face array allocation problems. To optimize this situation, the compiler simply sets its size to 14, so there is no trouble. Otherwise, a single object with a size of 13 and an array with a size of N is 14 * (n-1) + 13. (How do you solve the memory alignment problem if you design the compiler .) 3. class c006 {int set () {return 1 ;}; static int Foo () {return 2 ;}}; // sizeof = 1 class c007 {short m; // sizeof = 2 float N [3]; // sizeof = 4 int set () {}; static int sfoo () {return 1 ;}}; // sizeof = 16 common member functions, static member functions, and static member variables are not expressed in class objects. The association between member functions and objects is handled by the compiler during compilation. the compiler will determine the correct normal member function address during compilation and use the object address as the this pointer, as the first parameter, it is passed to the common member function for association. Static member functions are similar to global functions and are not associated with specific objects. The same is true for static member variables. Static member functions and static member variables are different from common global functions and global variables because they have a limited name. 4. the base class is empty and the derived class is empty class base {}; // sizeof = 1 class derived: public base {}; // sizeof = 1 5. the base class is empty and the derived class is not empty class base {}; // sizeof = 1 class derived: public base {float F; char C [11]; int Foo () {return 4 ;};}; // sizeof = 16 6. the base class is not empty and the derived class is empty. class base {float F; char C [11]; int Foo () {return 4 ;};}; // sizeof = 16 class derived: public base {}; // sizeof = 16 7. the base class is not empty and the derived class is not empty. class base {float F; char C [11]; Public: Int Foo () {return 4 ;};/// sizeof = 16 class derived: Private base {float F1; char c1 [11]; public: int foo1 () {return 4 ;};}; // The member variables of the sizeof = 32 base class are inherited by the derived class and have nothing to do with the inheritance method (public or private, for example, derived is private inherited from base. Inheritance only affects the "visibility" of data members ". The member variables inherited from the base class in the derived class object are initialized by the base class constructor. The default constructor is usually called unless the derived class explicitly calls the non-default constructor of the base class in its constructor initialization list. If no default constructor is specified and the parent class does not have a default constructor, a compilation error occurs. 8. multi-layer inheritance class level_1 {PRIVATE: Char C [3]; public: level_1 () {C [0] = 0x00; C [1] = 0x01; c [2] = 0x02 ;}; void set () {C [0] = 0x00; C [1] = 0x00; c [2] = 0x00 ;};}; // sizeof = 3 class level_2: Public level_1 {PRIVATE: int F; public: level_2 () {f = 7 ;}; void set () {f = 0 ;};}; // sizeof = 8 class level_3: Public level_2 {private: Short s [5]; public: level_3 () {for (INT I = 0; I <5; I ++) s [I] = I ;}; void set () {For (INT I = 0; I <5; I ++) s [I] = 0x400 ;};/// sizeof = 20 the output result is: the size of level_1 is 3The detail of level_1 is 00 01 02the size of level_2 is 8The detail of level_2 is 00 01 02 CC 07 00 00 00 00the size of level_3 is 20The detail of level_3 is 00 01 02 CC 07 00 00 00 00 00 01 00 00 03 00 04 00 CC therefore, regarding multi-layer inheritance, the object layout of the derived class is based on the data member alignment of the memory plus the data member in the derived class. The top-level class is in front of the class. 9. Modify the example in 8 to class level_1 {...}; Class level_2 {...}; Class level_3: Public level_1, public level_2 {...}; That is, only the inheritance relationship is modified, and the multi-level inheritance is converted into multi-level inheritance, which remains unchanged. The output result is: the size of level_3 is 20The detail of level_3 is 00 01 02 CC 07 00 00 00 00 00 01 00 02 00 03 00 04 00 CC, for multi-inheritance, the object layout of the derived class is still based on the data members in the base class. Based on the memory alignment, the data members in the derived class are added, and the left base class is in the front. 10. multi-level inheritance and multi-level inheritance class L1 {char c1 [2]; public: L1 () {c1 [0] = 0x01; c1 [1] = 0x02 ;};}; // sizeof = 2 class L2 {int I2 [2]; public: L2 () {I2 [0] = 7; i2 [1] = 8 ;};}; // sizeof = 8 class L3: Public L1, public L2 {char C3 [2]; public: L3 () {C3 [0] = 0x04; C3 [1] = 0x05 ;};/// sizeof = 16 class L4: Public L2 {int I4 [2]; public: L4 () {I4 [0] = 9; I4 [1] = 10 ;};}; // sizeof = 16 class L5: Public L3, public L4 {char C5; public: l5 () {C5 = 0x0a ;};}; // sizeof = 36. The output result is: the size of L1 is 2The detail of L1 is 01 02the size of L2 is 8The detail of L2 is 07 00 00 08 00 00 00 00 00the size of L3 is 16the detail of L3 is 01 02 CC 07 00 00 00 08 00 00 04 05 CC ccthe size of L4 is 16the detail of L4 is 07 00 00 00 08 00 00 00 09 00 00 00 00 0a 00 00 00the size of L5 is 36The detail of L5 is 01 02 CC 07 00 00 00 08 00 00 04 05 CC 07 00 00 0008 00 00 00 09 00 00 00 00 00 00 0a CC a derived class at a certain position in the hierarchy, and its instance object memory, it stores the memory content of each base class starting from the left base class, and finally its own memory content. This is a recursive process. Similar to traversing the inheritance tree in the direction of the base class.

 

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.