C + + Advanced -- program memory model - four memory areas

Posted by solarisuser on Fri, 04 Feb 2022 05:00:30 +0100

catalogue

c + + core programming

Memory partition model

1. Before program operation

2 after the program runs

3 new operator

c + + core programming

This stage mainly focuses on the detailed explanation of C + + object-oriented programming technology, and discusses the core and essence of C + +.

Memory partition model

When the C + + program is executed, the memory is divided into four areas

○ code area: it stores the binary code of the function body and is managed by the operating system

○ global area: store global variables, static variables and constants

○ stack area: it is freely allocated and released by the compiler to store the parameter values and local variables of the function

○ heap area: it is allocated and released by the programmer. If the programmer does not release it, it will be recovered by the operating system at the end of the program

Meaning of four memory areas:

The data stored in different areas gives us different life cycles and gives us greater flexibility in programming

1. Before program operation

After the program is compiled, an exe executable program is generated. Before the program is executed, it is divided into two areas

Code area:

1. Store the machine instructions executed by the CPU

2. The code area is shared. The purpose of sharing is to have only one code in the memory for the frequently executed program

3. The code area is read-only, so the reason why it is read-only is to prevent the program from accidentally modifying its instructions

Global area:

1. Global variables and static variables are stored here

2. The global area also contains a constant area, where string constants and other constants are also stored

3. The data in this area is released by the operating system after the program is completed

#include <iostream>
using namespace std;

//global variable
int g_a = 10;
int g_b = 10;

//const decorated global constant
static int c_g_a = 10;
static int c_g_b = 10;

int main(){
	
    //Global area
    
    //Global variable, static variable, constant

    //Create a normal 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_a Your address is" << (int)&g_a << endl;
    cout << "global variable g_b Your address is" << (int)&g_b << endl;

    //Static variables add static before ordinary variables, which 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;   

    //constant
    //string constant 
    cout << "The address of the string constant is:" << (int)&"hello world" << endl;

    //const modified variable
    //Const modified global variable const modified local variable

    cout << "Global constant c_g_a Your address is: " << (int)&c_g_a << endl;
    cout << "Global constant c_g_b Your address is: " << (int)&c_g_b << endl;

    const int c_l_a = 10; // c const g- global l- local
    const int c_l_b = 10;
    
    cout <<  "Local constant c_l_a Your address is: " << (int)&c_l_a << endl;
    cout <<  "Local constant c_l_b Your address is: " << (int)&c_l_b << endl;



    system("pause");

    return 0;
}

2 after the program runs

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 local variables. The data opened up in the stack area is automatically released by the compiler

#include <iostream>
using namespace std;

//Precautions for stack data -- do not return the address of local variables
//The data in the stack area is managed and released by the compiler

void func() // The formal parameter data will also be placed in the stack area
{
    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(){
	
    //Receive the return value of func function
    int * p = func();

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

    system("pause");

    return 0;
}

Stacking 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

In C + +, new is mainly used to open up memory in heap area

#include <iostream>
using namespace std;

int * func()
{
    //The new keyword can be used to open up data to the heap area
    //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 the heap area
    int *p = func();

    cout << *p << endl;
    cout << *p << endl;

    system("pause");

    return 0;
}

3 new operator

C + + uses the new operator to open up data in the heap area. The data opened up in the heap area is opened up manually by the programmer, released manually, and released using the delete operator

Syntax data type: new

The data created with new will return the pointer of the type corresponding to the data

#include <iostream>
using namespace std;

1,new Basic grammar of
int * func()
{
    //Create integer data in heap
    //new returns a pointer to change the data type
    int * p = new int(10);
    return p;
}

void test01()
{
    int * p = func();
    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;//The memory has been released. Accessing it again is an illegal operation and an error will be reported
}

//2. 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];//10 means that the array has 10 elements
    
    for(int i = 0; i < 10; i++)
    {
        arr[i] = i + 100;//Assign 100 ~ 109 to 10 elements
    }

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

int main(){
	test01();
    test02();
    

    system("pause");

    return 0;
}

Topics: C++ Back-end