Geekband C ++ advanced object-oriented programming-Week 4, Week 4

Source: Internet
Author: User

Geekband C ++ advanced object-oriented programming-Week 4, Week 4

# Question description:

The following types of Fruit and Apple are given respectively, and the reason for the size is explained by drawing an object model of the two.

class Fruit {    int no;    double weight;    char key;public:    void print() { }    virtual void process() { }};  class Apple: public Fruit {    int size;    char type;public:    void save() { }    virtual void process() { }};

# Data sorting:

Apple and Fruit object memory allocation:

* Note: The size of memory allocated by the compiler must be considered"Bit alignment", The rules are as follows:

1. Save the data objects in the class in sequence according to the declared order.

2. If there is a virtual function in the class, there will be a virtual pointer at the beginning of the object.

3. The starting offset of the variable is set to an integer multiple of its own byte size to make the cache faster when reading the variable.

4. After the memory allocation is complete, you need to add the current memory space to a multiple of the maximum basic type variables.

* Supplement: In the vs compiling environment, the starting address of the data member must be an integer multiple of the internal maximum data type. When there is a virtual pointer, you need to consider bitwise alignment.

# Job release:

Test and verification are carried out based on the consideration method of ye Karina.

Train of Thought: first, verify the location where the virtual pointer is stored in the memory and it occupies memory units. Second, verify the memory of each data object and its bit alignment.

Note whether the virtual pointer in the inheritance relationship is the new memory.

The test code is as follows:

#include <iostream>using namespace std;class Fruit {public:    int no;                                    double weight;                               int key;                               public:    void print() { }    virtual void process() {}};class Apple : public Fruit {public:    int size;                                 int type;public:    void save() {   }    virtual void process() {   }};int main() {    cout << "sizeof(Fruit)   = " << sizeof(Fruit) << endl;    cout << "sizeof(Apple)   = " << sizeof(Apple) << endl;    Fruit fruit;    Apple apple;    cout << "Fruit           = " << &fruit << endl;    cout << "Fruit.no        = " << &fruit.no << endl;    cout << "Fruit.weight    = " << &fruit.weight << endl;    cout << "Fruit.key       = " << &fruit.key << endl;    cout << "Apple           = " << &apple << endl;    cout << "Apple.no        = " << &apple.no << endl;    cout << "Apple.weight    = " << &apple.weight << endl;    cout << "Apple.key       = " << &apple.key << endl;    cout << "Apple.size      = " << &apple.size << endl;    cout << "Apple.type      = " << &apple.type << endl;    system("pause");    return 0;}

 

The running result of win10 vs2017 is as follows:

sizeof(Fruit)   = 32sizeof(Apple)   = 40Fruit           = 003DF8D4Fruit.no        = 003DF8DCFruit.weight    = 003DF8E4Fruit.key       = 003DF8ECApple           = 003DF8A4Apple.no        = 003DF8ACApple.weight    = 003DF8B4Apple.key       = 003DF8BCApple.size      = 003DF8C4Apple.type      = 003DF8C8

* Supplement: If a virtual function (including inheritance) exists in the class, there must be only one virtual function pointer pointing to the virtual table, and the virtual function address is stored in the virtual table.

# Object model:

According to the summary and test code, the object model is as follows:

 

# Supplement: the memory size of objects built in the heap or stack is the same.

 

 

 

 

 

 

 


 

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.