Memory partition model of C++ programming (code area, global area, stack area, heap area)

foreword

I have learned it before and made notes. I will sort out and supplement the notes again, and review it myself. I hope it will be helpful to everyone.


memory partition model

  1. Code area : store the binary code of the function body, managed by the operating system
    All stored codes
  2. Global area : store global variables and static variables and constants
  3. Stack area : automatically allocated and released by the compiler. Store function parameter values, local variables, etc.
  4. Heap area : allocated and released by the programmer, if the programmer does not release it, it will be reclaimed by the operating system at the end of the program

The code area and the global area are an area before the program runs
, while the heap area and the stack area are an area after the program runs

The meaning of the four memory areas:
The data stored in different areas are endowed with different life cycles, allowing us to program flexibly.


Before the program runs

After the program is compiled, a compilable exe executable program is generated, which is divided into two areas before the program is executed

code area

  • Stores machine instructions executed by the CPU
  • The code area is shared . The purpose of sharing is that for frequently executed programs, only one copy of the code is required in the memory.
  • The code area is read-only . The reason for reading-only is to prevent the program from accidentally modifying its instructions.

global zone

  • Store global variables and static variables
  • The global area also includes the constant area , which also stores string constants and other constants (global variables modified by const)
  • The data in this area is released by the operating system after the program finishes running

As long as there are locally modified variables or constants, they are not in the global area.
For example: local variables modified by const

/*
 * @Author: _oufen
 * @Date: 2022-11-17 00:29:53
 * @LastEditTime: 2022-11-17 00:51:52
 * @Description:
 */
#include <iostream>
using namespace std;

//全局变量
int global_a = 10;
int global_b = 10;

/*全局常量*/
const int c_g_a = 10;
const int c_g_b = 10;

int main()
{
    
     /*全局变量 静态变量 常量 存放在全局区*/
    // 局部变量
    int a = 10;
    int b = 10;
    cout << "局部变量a的地址:" << (long long)&a << endl;
    cout << "局部变量b的地址:" << (long long)&b << endl;

    /*全局变量*/
    cout << "全局变量a的地址:" << (long long)&global_a << endl;
    cout << "全局变量b的地址:" << (long long)&global_b << endl;

    /*静态变量*/
    static int sta_a = 10;
    static int sta_b = 10;
    cout << "静态变量a的地址:" << (long long)&sta_a << endl;
    cout << "静态变量b的地址:" << (long long)&sta_b << endl;

    /*常量*/
    // 1. 字符串常量
    cout << "字符串常量的地址为:" << (long long)&"helloworld" << endl;
    // 2. const 修饰的局部变量
    const int con_a = 10;
    const int con_b = 10;
    cout << "const 修饰变量a的地址:" << (long long)&con_a << endl;
    cout << "const 修饰变量b的地址:" << (long long)&con_b << endl;

    /*全局常量*/
    cout << "全局常量c_g_a的地址 :" << (long long)&c_g_a << endl;
    cout << "全局常量c_g_b的地址 :" << (long long)&c_g_b << endl;
    system("pause");
    return 0;
}

Run the screenshot:
insert image description here

通过上面的例子可以总结出
The global zone stores :

  • global variable int c_g_a=10;
  • static variable static int sta_a=10;
  • constantincludestring constant "helloworld"andglobal constant const int c_g_a=10;

Summarize:

  1. C++ is divided into global area and code area before the program runs
  2. The code area is characterized as shared and read-only
  3. The global area stores global variables static variables string constants const-modified global variables

After the program runs

stack area:
Automatically allocated and released by the compiler, storing function parameter values, local variables, etc.

Precautions,Do not return the address of a local variable, the data created in the stack area is automatically released by the compiler

/*
 * @Author: _oufen
 * @Date: 2022-11-17 10:55:15
 * @LastEditTime: 2022-11-17 10:56:46
 * @Description:栈区
 */
#include <iostream>

using namespace std;
int *funca(int b) //形参也存放在栈区
{
    
    
    int f_a = 10; //局部变量 存放在栈区,栈区中的数据在函数执行完后自动释放
    return &f_a;  //返回局部变量的地址 会报错
    // warning: address of local variable 'a' returned [-Wreturn-local-addr]
}
int main()
{
    
    
    int *p = funca(1);
    cout << "*p = " << *p << endl;
    cout << "*p = " << *p << endl;
    system("pause");
    return 0;
}

heap area:
allocated and released by the programmer, if the programmer does not release it, it will be reclaimed by the operating system at the end of the program

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

The data in the heap area will not be released, and you can use the pointer to point to the heap area to use the data in the heap area.
The data in the heap area is allocated and released by the programmer

/*
 * @Author: _oufen
 * @Date: 2022-11-17 10:55:15
 * @LastEditTime: 2022-11-17 11:10:57
 * @Description:堆区
 */
#include <iostream>

using namespace std;
int *func()
{
    
    
    /*利new关键字,可以将数据开辟到堆区*/
    //指针也是局部变量 放在栈区 指针保存的数据放在堆区
    //堆区的地址用栈上的指针保存
    // new关键字可以创建堆区的数据
    // 返回的是创建堆区数据的地址
    int *p = new int(10); // new int (10)  在堆区开辟了一段内存,将这段内存的编号返回,指针p接收这段内存
    return p;
}
int main()
{
    
    
    //堆区开辟数据
    int *p = func();
    cout << "*p = " << *p << endl;
    cout << "*p = " << *p << endl;
    cout << "*p = " << *p << endl;
    cout << "*p = " << *p << endl;
    system("pause");
    return 0;
}

The new operator opens up heap data

In c++, the new operator is used to open up data in the heap area , and the stored data is in the heap area.

The data opened up in the heap area is manually opened up by the programmer and released manually, and the release uses the operatordelete

grammar:

new data type
The data created by new will return a pointer of the corresponding data type

/*
 * @Author: _oufen
 * @Date: 2022-11-17 11:19:37
 * @LastEditTime: 2022-11-17 11:32:40
 * @Description: new操作符开辟堆区数据
 */
#include <iostream>
using namespace std;
int *func1()
{
    
    
    //在堆区创建整形数据
    // new返回的是创建堆区数据类型的指针
    int *p = new int(10);
    return p;
}
void func2()
{
    
    
    //在堆区使用new开辟数组
    // int *arr = new int[10]; 在堆区开辟了空间为10的数组
    int *array = new int[5]{
    
    1, 2, 3, 4, 5};
    for (int i = 0; i < 5; i++)
    {
    
    
        cout << array[i] << endl;
    }

    //释放堆区的数组
    //释放数组的时候,要加[]

    delete[] array;
}
int main()
{
    
    
    int *p = func1();
    // 堆区的数据由程序猿来 开辟 也由程序猿来 释放
    // 可以使用delete关键字来释放堆区数据
    cout << "*p = " << *p << endl;
    cout << "*p = " << *p << endl;
    cout << "*p = " << *p << endl;
    delete (p); //释放了堆区数据
    // cout << "*p = " << *p << endl; //*p = 900995024

    func2();

    system("pause");
    return 0;
}

Summarize:

  1. Use the delete keyword to release the data in the heap area
  2. When releasing the pointer, directly delete p;
  3. When releasing data, delete[] array;
  4. The data stored in the heap area is manually created (new) and released (deleted) by the programmer

Guess you like

Origin blog.csdn.net/cyaya6/article/details/128218361