Memory Distribution (upper) and VS distribution of classes in

Source: Internet
Author: User

Memory Distribution (upper) and VS distribution of classes in

0. Order

I am currently studying C ++. I am also interested in the Implementation Principles of C ++ classes and their classes. So we plan to better understand the implementation of classes by observing the class distribution in the memory. In fact, the distribution of classes is determined by the compiler, And the compiler used in this experiment is VS2015 RC, so the title here is "Memory Distribution of classes in VS".

1. Exploration of non-inheritance classes

 1.1 empty class

Let's start with an empty class step by step.

 

// Empty class test {};

 

int main(int argc, char *argv[]){    test ts;    cout << sizeof(ts) << endl;    return 0;}

The result is 1.

Therefore, we speculate that for an empty class, the memory will always allocate a byte space for it. For this reason, we can verify that:

 

int main(int argc, char *argv[]){    test ts;    char ch = '0';    int a1, a2;    a1 = (int)(&ts);    a2 = (int)(&ts + 1);    memcpy(&ts, &ch, 1);    cout << sizeof(ts) << endl;    return 0;}

 

We can see that a1 is the ts address (forcibly converted to int), a2 is the next address of ts, and then we will go to the memory to check it.

The result is actually written into the ts.

In summary, we can draw a conclusion that for an empty class, the compiler will always allocate a byte of memory to it.

  1.2 classes containing only data members

First, the data member is a byte (char) class. Through the above test code, the result is the same as that of an empty class. The Compiler allocates a byte space to the char in the class. This is within our expectation.

However, when our classes are designed to contain different types of data structures, the results will be different:

class test{public:    char c;    int i;};

The output result of the program is 8. We can see that the class occupies 8 bytes of memory.

This involves memory alignment. So next we will first discuss the "memory alignment" in C ++ ".

Memory alignment:

For a class (struct), the compiler has a rule called "memory alignment" to improve the memory read rate and portability. Generally, for memory alignment, the compiler will help you, but this kind of work can be done by the programmer.

In C ++, you can use# Pragma pack (n)To set the alignment coefficient ". (The default alignment coefficient is 8 in VC .)

To better understand the memory alignment process in the memory, I specially drew a flowchart for allocating space. (I only understand it myself. If there are any mistakes, please point them out .)

 

The following example shows the memory alignment. (Here, we only consider the case where the data member is not a class (struct)

 

 

class test{public:    char c;    int i;    short s;};

Although the content of the class is the same, the allocation in the memory will be different because of the alignment factor n. It can be illustrated visually. Red indicates char type, blue indicates int type, and Green indicates short type. The number of columns in the figure is determined by min (max (data type in the struct), n.
(1) Example 1:Pragma pack (1)

(2) Example 2:Pragma pack (2)

 

(3) Example 3:Pragma pack (4)

 

We can see that different alignment coefficients make the memory distribution look different.

 

After discussing the memory alignment, let's take a look at the static members in the class.

Static member in the class:

We design a class that includes static data members.

 

class test{public:
static int si; char c; int i; short s;};

 

The result shows that the size of the class is the same as before.

At the same time, we can use the cout statement to view the static member address in the class.

 

cout << sizeof(ts) << '\n' << (int)&ts.si << '\n' << (int)(&ts) << endl;

 

 

 

  

 

The result shows that the class address is 108,000 different from the static member address of the class. Obviously, static members in the class are not stored together with the class.

In conclusion, we can conclude that only non-static member data in the class can open up memory space for it.

 1.3 class containing member functions

For member functions in the class, we have two kinds of conjecture: The first conjecture is that a piece of memory must be opened up in each class to store member functions in the class, each time an external call is made to a class member function, the function is accessed through the function pointer in the category class. The second conjecture is that the member functions in the class are independent, and each class does not store information about the member functions, the member function is called by the compiler to automatically select the function to be accessed during compilation. To this end, we also need to perform some tests.

 

class test{public:    static int si;    char c;    int i;    short s;    test():c('0'),i(0),s(0) {};    void print(void) { cout << sizeof(test) << '\n' << (int)&si << '\n' << (int)this << endl; }};

 

 

 

  

The output results show that the memory remains unchanged, indicating that the member functions do not occupy space in the class memory.

In conclusion, it can be concluded that member functions without inherited classes are independent, and the corresponding function information is not stored in the class memory. Access to member functions is completed through the compiler.

 

However, here we have not considered the existence of a virtual function. This is a special case where the memory will be allocated with the corresponding space. I will not discuss it here, and I will take a look at the specific analysis in the next article.

 

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.