Simple analysis of the C + + object model and memory bit alignment (GNU gcc&vs2015 compiler)

Source: Internet
Author: User

Take fruit and Apple as an example to analyze:

Fruit and Apple are defined as follows:

By testing in two compilation environments (GNU GCC & VS2015), you can see that the object model of both compilers is the same, as shown in:

Apple is a subclass of fruit, which is a two-level single-chain inheritance structure. Within Apple and fruit objects, the following principles are followed:

    1. The first member of an object is a virtual pointer to a virtual table;
    2. Objects are stored in the order in which they are declared;

However, the memory bit alignment of the two compilers is slightly different.

For the GNU GCC compiler, it follows the following guidelines:

    1. Memory allocation in the order in which they appear in the Declaration
    2. The starting offset address of a variable must be a multiple of its own size, for example, in Struct{bool a;double b;}, A is 1 bytes, whereas B is 8 bytes, so B must be from the location where the address is compiled 8, that is, A and B are empty 7 bytes. This is designed because reading a variable in this way can make the cache faster.
    3. If there is a virtual function in the class, there will be a virtual pointer at the beginning of the object.
    4. After the memory allocation of all variables ends, the object is to be filled into memory as a multiple of the largest primitive type variable. For example, if the largest basic type in a class is double, then it needs to be filled in at the end of the integer multiples of 8.

There are also three features that are not involved in the relationship between fruit and apple, and they are:

    1. In the case of multiple inheritance, there will be different vptr on the front of each base class;
    2. If a new virtual function exists in a derived class, it produces a virtual table that is compatible with the base class, without adding a new table;
    3. When you combine relationships, the starting address of an inner class starts with an integer multiple of the largest base data type of the inner class.

Combined with the first 4 features, can be calculated in the GNU GCC compilation environment, the size of the fruit is (4+4+8+ (1+7)) =24bytes; Apple's size is (24+4+ (1+3)) =32bytes, such as:

However, in the VS2015 compilation environment, the virtual pointer bit alignment is different. The starting address of a data member in VS2015 must also be an integer multiple of the internal maximum base data type, meaning that there must be 4 placeholder bytes between the virtual pointer and the data member, so the size of the fruit is ((4+4) + (4+4) +8+ (1+7)) =32bytes; And the size of Apple is (32+4+ (1+4)) = 40;

, although the size of the pointer variable is 4 bytes, the offset of no is still starting from 8.

Apple's memory is as follows:

First 32 Bytes:

After 8 bytes:

Where the 0X649BFB00 bit virtual pointer address, 0XCC is a placeholder, the first 0xFFFFFFFF is no,0x0000000000000000 for weight,0x01 as key.

Note: If you delete a double variable, the first data member starts at 4, which means that the size of the fruit should change to (4+4+ (1+3)) = 12,

The memory allocation diagram is as follows:

This verifies the additional condition of the VS2015 compiler.

Simple analysis of the C + + object model and memory bit alignment (GNU gcc&vs2015 compiler)

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.