C ++ learning (5) - partition model of memory

Memory partition model

C ++ program is executed, the general direction of the memory is divided into four regions

  • Code area: storing binary code function body, managed by the operating system
  • Global Area: store global variables and static variables and constants
  • Stack Area: allocated by the compiler automatically released, the stored function parameters, local variables, etc.
  • Heap: the allocation and release by the programmer. If the programmer does not release at the end of the program by the operating system recovery

Memory significance of four areas :

  • Data stored in different areas, giving different life cycles, giving us greater flexibility program

1.1 before running

After compiling the program, to generate the .exeexecutable program prior to execution of the program is not divided into two regions

Code area:

  • The machine instruction executed by the CPU

  • Code area is shared, the shared goal is for the program to be executed frequently, you only need to have a code can be in memory

  • Code area is read-only, read-only because it prevents accidental modification of its program instruction

Global Area:

  • Global and static variables are placed in this

  • Global zone also contains a constant region, string constants, and other constants are also placed in this

  • The data area is freed by the operating system after the end of the program

    #include<iostream>
    using namespace std;
    
    //创建全局变量
    int g_a = 10;
    int g_b = 10;
    //const修饰的全局常量
    const int c_g_a = 10;
    const int c_g_b = 10;
    
    int main(){
        //全局区
        //全局变量、静态变量、常量
    
        //创建普通局部变量
        int a = 10;
        int b = 10;
        cout << "局部变量a的地址为:" << (int)&a << endl;
        cout << "局部变量b的地址为:" << (int)&b << endl;
    
        cout << "全局变量g_a的地址为:" << (int)&g_a << endl;
        cout << "全局变量g_b的地址为:" << (int)&g_b << endl;
    
        //静态变量
        static int s_a = 10;
        static int s_b = 10;
        cout << "全局变量s_a的地址为:" << (int)&s_a << endl;
        cout << "全局变量s_b的地址为:" << (int)&s_b << endl;
    
        //常量
        //字符串常量
        cout << "字符串常量的地址为:" << (int)&"helloworld" << endl;
        //const修饰的全局变量
        cout << "全局常量c_g_a的地址为:" << (int)&c_g_a << endl;
        cout << "全局常量c_g_b的地址为:" << (int)&c_g_b << endl;
        //const修饰的局部变量
        const int c_l_a = 10;
        const int c_l_b = 10;
        cout << "全局常量c_l_a的地址为:" << (int)&c_l_a << endl;
        cout << "全局常量c_l_b的地址为:" << (int)&c_l_b << endl;
    
        return 0;
    }

    Overall situation

to sum up:

  • C ++ is divided into zones and the global zone before running the code
  • Code area is characterized by shared and read-only
  • Global Area stored in global variables, static variables, constants
  • Constant region stored const modified global constants and string constants

After running 1.2

Stack area:

  • Automatically allocated by the compiler release, storage parameter value of the function, local variables, etc.

  • Note: Do not return address local variables, stack area to open up data automatically released by the compiler

    • Local variables in a stack area, the data area of ​​the stack is automatically released after the completion of the function performed
    #include<iostream>
    using namespace std;
    
    //栈区数据的注意事项 —— 不要返回局部变量的地址
    //栈区数据由编译器管理开辟和释放
    int* func(){
        int a = 10;
        return &a;
    }
    
    int main(){
        int *p = func();
        cout << *p << endl;
        return 0;
    }

Heap:

  • Assigned by the programmer release, if the programmer does not release at the end of the program by the operating system recovery

  • The main use in C ++ new open memory in the heap area

    #include<iostream>
    using namespace std;
    
    
    int* func(){
        //利用new关键字可以将数据开辟到堆区
        //指针的本质也是局部变量,放在栈上,指针保存(指向)的数据是放在堆区
        int * p = new int (10);
        return p;
    }
    
    int main(){
        //在堆区开辟数据
        int *p = func();
        cout << *p << endl;
        return 0;
    }

1.3 new operator

  • In C ++ using the new operator data in the heap area open

  • Open heap data manually opened by the programmer, manual release, the release using operator delete

  • grammar:new 数据类型

  • Using the new data created corresponding to the data returned pointer type

    #include<iostream>
    using namespace std;
    
    //1.new的基本语法
    int * func(){
        //在堆区创建整型数据
        //new返回的是该数据类型的指针
        int *p = new int(10);
        return p;
    }
    
    void test01(){
        int *p = func();
        cout << *p << endl;
        delete p;
        cout << *p << endl;
    }
    
    void test02(){
        //创建10整型数据的数组,在堆区
        int *arr = new int[10];
    
        for(int i=0;i<10;i++){
            arr[i] = i+100;
        }
        foe(int i=0;i<10;i++){
            cout <<arr[i]<<endl;
        }
        //释放数组时加中括号
        delete[] arr;
    }
    
    int main(){
        //在堆区开辟数据
        test01();
        return 0;
    }

Guess you like

Origin www.cnblogs.com/maeryouyou/p/11968487.html