C + + memory partition model

Keywords: C C++

This stage pays attention to the detailed explanation of C + + object-oriented programming calculation, and discusses the core and essence of C + +.
1. Memory partition model
During the execution of C + + program, the general direction of memory is divided into four areas:
1) Code area: it stores the binary code of the function body and is managed by the operating system;
2) Global area: stores global variables, static variables and constants.
3) Stack area: automatically allocated and released by the compiler to store function parameter values, local variables, etc;
4) Heap area: it is allocated and released by the programmer. If the programmer does not release it, it will be recycled by the operating system at the end of the program.
Meaning of four memory areas:
The data stored in different areas are given different declaration cycles, giving us greater flexibility in programming.
2. Before running the program
After the program is compiled, the exe executable program is produced. Before the program is changed, it is divided into two areas.
Code area:
Store machine instructions executed by CPU;
The code area is shared. The purpose of sharing is to correspond to the frequently executed programs. You only need to have a code in memory;
The code area is read-only because it prevents the program from accidentally modifying its instructions.
Global area:
Global and static variables are stored here.
The global area also contains a constant area, where string constants and other constants are also stored.
The data in this area is released by the operating system after the program ends.

#include <iostream>
using namespace std;

int g_c = 10;
int g_d = 10;

int main()
{
    //Ordinary local variable
    int a = 10;
    int b = 10;

    cout << "local variable a Your address is:" << (int)&a << endl;
    cout << "local variable b Your address is:" << (int)&b << endl;
    cout << "global variable g_c Your address is:" << (int)&g_c << endl;
    cout << "global variable g_d Your address is:" << (int)&g_d << endl;

    //Static variables, which add static before ordinary variables, belong to static variables
    static int s_a = 10;
    static int s_b = 10;
    cout << "Static variable s_a Your address is:" << (int)&s_a << endl;
    cout << "Static variable s_b Your address is:" << (int)&s_b << endl;

    system("pause");
    return 0;
}

Stack area:
It is automatically allocated and released by the compiler to store the parameter values and local variables of the function.
Note: do not return the address of the local variable. The data opened up in the stack area is automatically released by the compiler.

#include <iostream>
using namespace std;

//Stack data considerations 
//--Do not return the address of a local variable
//--The data in the stack area is managed and released by the compiler


/// <summary>
///The formal parameter data will also be placed in the stack area
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
int* func(int b)
{
	b = 100;
	int a = 10;  //Local variables are stored in the stack area, and the data in the stack area is automatically released after the function is executed
	return &a;  //Returns the address of a local variable
}

int main()
{
	//Accepts the return value of the func function
	int* p = func(1);

	cout << *p << endl;  //The first time you can print the correct number, it is because the compiler has made a reservation
	cout << *p << endl;  //The second time, the data is no longer retained

	system("pause");
	return 0;
}

Heap area
It is distributed by the programmer and released by the operating system when the programmer does not release the program.
C + + mainly uses new to open up memory in the heap.

#include <iostream>
using namespace std;

int* func()
{
    //The new keyword can be used to open up data to the heap
    //The pointer is also a local variable in nature. It is placed on the stack, and the data saved by the pointer is placed on the heap
    int* p = new int(10);
    return p;
}

int main()
{
    //Open up data in heap area
    int* p = func();

    cout << *p << endl;

    system("pause");

}

new operator
Using new operation in C + + to open up data in heap area;
Heap development data is manually developed by the programmer and released by the operator delete.
Syntax: new data type;
The data created with new will return the pointer of the type corresponding to the changed data.

#include <iostream>
using namespace std;

//Basic syntax of new
int* func()
{
    //Integer data in heap scene
    //new returns a pointer to the data type
    int* p = new int(10);
    return p;
}

void test01()
{
    int* p = func();
    cout << *p << endl;
    cout << *p << endl;

    //The data in the heap area is managed by the programmer and released by the programmer
    //If you want to release the data in the heap, use the keyword delete
    delete p;
    cout << *p << endl;
}

//Using new to open up arrays in heap
void test02()
{
    //Create an array of 10 integer data in the heap area
    int* arr = new int[10];

    for (int i = 0; i < 10; i++)
    {
        arr[i] = i + 100;
    }

    for (int i = 0; i < 10; i++)
    {
        cout << arr[i] << endl;
    }
    //Free heap array
    //You need [] to release the array
    delete[] arr;
}

int main()
{
    test01();
    test02();
    system("pause");  
}

Posted by mr_mind on Sat, 02 Oct 2021 18:52:45 -0700