C++:内存存储模型

系列文章目录

C++内存存储模型

C++引用以及函数的占位、重载

C++封装与对象特性

C++对象特性及友元 

C++运算符重载及继承

C++多态

C++文件操作

C++模板


前言

C++在执行程序时会把内存划分为4块区域,为了我们高效地编程,而这些区域的作用是什么呢?下面就由我来介绍介绍

一、内存概述

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

 

二、内存四区

1.代码区

  • 存放CPU执行的机器指令
  • 代码区是共享的,共享的目的是对于频繁被执行的程序,只需要在内存中有一份代码即可
  • 代码区是只读的,使其只读的原因是防止程序意外地修改了它的指令

2.全局区解析

2.1测试程序

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

//全局变量
int global_a;
int global_b;
//全局常量
const int global_con_a = 10;
const int global_con_b = 10;

int main()
{
    //局部变量
    int a;
    int b;
    cout << "局部变量a的地址为:" << reinterpret_cast<uintptr_t>(&a) << endl;
    cout << "局部变量b的地址为:" << reinterpret_cast<uintptr_t>(&b) << endl;
    //局部常量
    const int con_a = 10;
    const int con_b = 10;
    cout << "局部常量con_a的地址为:" << reinterpret_cast<uintptr_t>(&con_a) << endl;
    cout << "局部常量con_b的地址为:" << reinterpret_cast<uintptr_t>(&con_b) << endl;
    //全局变量
    cout << "全局变量a的地址为:" << reinterpret_cast<uintptr_t>(&global_a) << endl; 
    cout << "全局变量b的地址为:" << reinterpret_cast<uintptr_t>(&global_b) << endl;
    //静态变量
    static int sta_a;
    static int sta_b;
    cout << "静态变量sta_a的地址为:" << reinterpret_cast<uintptr_t>(&sta_a) << endl;
    cout << "静态变量sta_b的地址为:" << reinterpret_cast<uintptr_t>(&sta_b) << endl;
    //局部静态变量
    static int sta_loc_a;
    static int sta_loc_b;
    cout << "局部静态变量sta_loc_a的地址为:" << reinterpret_cast<uintptr_t>(&sta_loc_a) << endl;
    cout << "局部静态变量sta_loc_b的地址为:" << reinterpret_cast<uintptr_t>(&sta_loc_b) << endl;
    //静态常量
    static const int sta_con_a = 10;
    static const int sta_con_b = 10;
    cout << "静态常量sta_con_a的地址为:" << reinterpret_cast<uintptr_t>(&sta_con_a) << endl;
    cout << "静态常量sta_con_b的地址为:" << reinterpret_cast<uintptr_t>(&sta_con_b) << endl;
    //字符串常量
    cout << "字符串常量的地址为:" << reinterpret_cast<uintptr_t>(&"hello world") << endl;
    //全局常量
    cout << "全局常量global_con_a的地址为:" << reinterpret_cast<uintptr_t>(&global_con_a) << endl;
    cout << "全局常量global_con_b的地址为:" << reinterpret_cast<uintptr_t>(&global_con_b) << endl;
    return 0;
}

2.2输出

 从输出结果可以看出局部变量和局部常量是紧挨着的,存储在栈区并且存储方式是由高到低的,全局变量、静态变量、局部静态变量是紧挨着的,存储在全局区并且存储方式是由低到高的,静态常量、字符串常量、全局常量是紧挨着的,存储在常量区并且存储方式是由低到高的。

2.3总结

  • 全局变量和静态变量存放在此
  • 全局区还包含了常量区、字符串常量和其他常量也存放在此
  • 该区域的数据在程序结束后由操作系统释放

 3.栈区解析

3.1测试程序

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

int* func()
{
    int a = 10;
    return &a;
}

int main()
{
    int* p = func();
    cout << *p << endl;
    cout << *p << endl;
    return 0;
}

3.2输出

3.3总结

  • 局部变量和局部常量存储在栈区
  • 全局变量、静态变量、局部静态变量存放在全局区
  • 静态常量、字符串常量、全局常量存储在常量区

 4.堆区解析

3.1测试程序

在堆区开辟内存时需要用new关键字,而释放内存时需要用delete关键字

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

int* func()
{
    int* p = new int(10);//在堆区创建整型数据
    return p;
}

int* func2()
{
    int* arr = new int[10];//在堆区创建数组
    for (int i = 0; i < 10; i++)
    {
        arr[i] = i + 100;
    }
    return arr;
}

int main()
{
    int* p = func();
    cout << *p << endl;
    int* arr = func2();
    for (int i = 0; i < 10; i++)
    {
        cout << arr[i] << endl;
    }
    //释放堆区数据
    delete p;
    delete[] arr;
    return 0;
}

4.2输出

10
100
101
102
103
104

3.总结

以上就是今天要讲的内容,本文简单介绍了C++中内存的四大分区,并且用代码更直观地表现了数据在内存中的存储方式。

  • 12
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值