This article originates from: http://blog.csdn.net/ljianhui/article/details/45903939
In C + +, there are two kinds of member variables: static and non static, and three member functions: static, non static, and virtual. So how do they affect the distribution of C + + objects in memory? What is the memory distribution when inheritance is present?
Here's a very simple class that analyzes the effects of these two member variables and three member functions on the memory distribution of the objects of the class by gradually adding various members to it.
Note: The following code test results are based on the Ubuntu 14.04 64-bit system under the g++ 4.8.2, if on other systems or using other compilers, may run different results.
1. Memory distribution of objects containing classes with non-static member variables and member functions
The class persion is defined as follows:
[CPP]View Plain copy print? Class Person {Public:person (): mId (0), MAge {} void print () {cout << "ID:" << mId << ", Age:" << mAge << Endl; } Private:int mId; int mAge; };
The person class contains two non-static int-type member variables, a constructor, and a non-static member function. To figure out the memory distribution of objects in the class, do something about the object of that class:
[CPP] View plain copy print? Int main () { Person p1; cout << "sizeof (p1) == " << sizeof (p1) << endl ; int *p = (int*) &p1; cout << "p.id == << *p << ", address: " << p << endl; ++p; cout << "p.age == " << *p < < ", address: " << p << endl; cout << endl; person p2; cout << "sizeof (p2) == " << sizeof (p1) << endl; p = (int*) &p2; cout << "p.id == " << *p << ", address: " << p << endl ; ++p; cout << "P.age == " << *p << ", address: " << p << endl; return 0; }
The results of the operation are as follows:
From the image above, you can see that the object of the class occupies 8 bytes of memory, using a normal int* pointer to traverse the value of a non static member variable within the output object, and that the address of the same non-static member variable in the two objects varies.
As a result, it can be concluded that in C + +, non-static member variables are placed in each class object, and the non static member functions are placed outside the object of the class, and the order in which the non-static member variables are stored in memory is the same as in the class. The memory distribution of the person object is shown in the following illustration:
2. Memory distribution of objects of classes containing static and non-static member variables and member functions
Add a static member variable and a static member function to the person class, as follows:
[CPP] View plain copy print? class person { public: person (): mId (0), mage { ++scount; } ~person () { --scount; } void print () { cout << "id: " << mId << ", age: " &n
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