• 内存分区模型 安静点


    1 内存分区模型

    C++程序在执行时,将内存大方向划分为4个区域

    • 代码区:存放函数体的二进制代码,由操作系统进行管理的
    • 全局区:存放全局变量和静态变量以及常量
    • 栈区:由编译器自动分配释放, 存放函数的参数值,局部变量等
    • 堆区:由程序员分配和释放,若程序员不释放,程序结束时由操作系统回收

    内存四区意义:

    不同区域存放的数据,赋予不同的生命周期, 给我们更大的灵活编程

     

    1.1 程序运行前

    ​ 在程序编译后,生成了exe可执行程序,未执行该程序前分为两个区域

    ​ 代码区:

    ​ 存放 CPU 执行的机器指令

    ​ 代码区是共享的,共享的目的是对于频繁被执行的程序,只需要在内存中有一份代码即可

    ​ 代码区是只读的,使其只读的原因是防止程序意外地修改了它的指令

    ​ 全局区:

    ​ 全局变量和静态变量存放在此.

    ​ 全局区还包含了常量区, 字符串常量和其他常量也存放在此.

    ​ ==该区域的数据在程序结束后由操作系统释放==.

     

    //全局变量
    int g_a = 10;
    int g_b = 10;
    
    //全局常量
    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)&"hello world" << endl;
        cout << "字符串常量地址为: " << (int)&"hello world1" << endl;
    
        cout << "全局常量c_g_a地址为: " << (int)&c_g_a << endl;
        cout << "全局常量c_g_b地址为: " << (int)&c_g_b << endl;
    
        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;
    
        system("pause");
    
        return 0;
    }

    结果:

     总结:

    • C++中在程序运行前分为全局区和代码区
    • 代码区特点是共享和只读
    • 全局区中存放全局变量、静态变量、常量
    • 常量区中存放 const修饰的全局常量 和 字符串常量

    1.2 程序运行后

    ​ 栈区:

    ​ 由编译器自动分配释放, 存放函数的参数值,局部变量等

    ​ 注意事项:不要返回局部变量的地址,栈区开辟的数据由编译器自动释放 

     示例:

    int* func()
    {
        int  a = 10;
        return &a;//返回局变量a的地址
    }
    
    int main() {
    
        int *p = func();
    //在栈区的数据在func函数执行完成之后就自动释放了,拿到局部变量的地址也没什么用
        cout << *p << endl;//第一次没问题,因为会保留一次
        cout << *p << endl;//这时候会出问题,因为顶多保留一次
        
        system("pause");
    
        return 0;
    }

     堆区:

    ​ 由程序员分配释放,若程序员不释放,程序结束时由操作系统回收

    ​ 在C++中主要利用new在堆区开辟内存

    int* func()
    {
        //1 利用new在堆区开辟内存,但是此时的局部变量指针a还是放到了栈中,只不过a的数据开辟在了堆中
        int* a = new int(10);
        return a;
    }
    
    int main() {
    
        int *p = func();
        //2  *p解引用,此时调用多少次都可以,因为堆中的数据释放是由程序员手动释放或者关闭程序的运行的时候也会进行释放。 
        cout << *p << endl;
        cout << *p << endl;
        
        system("pause");
    
        return 0;
    }

     

     

    //全局变量int g_a = 10; int g_b = 10; //全局常量constint c_g_a = 10; constint 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; //静态变量staticint s_a = 10; staticint s_b = 10; cout << "静态变量s_a地址为: " << (int)&s_a << endl; cout << "静态变量s_b地址为: " << (int)&s_b << endl; cout << "字符串常量地址为: " << (int)&"hello world" << endl; cout << "字符串常量地址为: " << (int)&"hello world1" << endl; cout << "全局常量c_g_a地址为: " << (int)&c_g_a << endl; cout << "全局常量c_g_b地址为: " << (int)&c_g_b << endl; constint c_l_a = 10; constint c_l_b = 10; cout << "局部常量c_l_a地址为: " << (int)&c_l_a << endl; cout << "局部常量c_l_b地址为: " << (int)&c_l_b << endl; system("pause"); return0; }

  • 相关阅读:
    1010 幂次方
    1316 丢瓶盖
    1182 数列分段2
    Mysql动态SQL语句标签
    知了CMS开发说明文档(ibeetl) 建站系统文档
    BeanUtils.populate()的用法
    如何将网页的title前面的图标替换成自己公司的图标
    HTTP、HTTPS常用的默认端口号
    项目的xml文件中经常使用的sql语句
    Invalid bound statement(not found): com.xxx.xxx.xxx.xxxMapper.save
  • 原文地址:https://www.cnblogs.com/anjingdian/p/16030906.html
Copyright © 2020-2023  润新知