1、内存分区模型

 

 

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

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

 

内存四区的意义:

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

1.1 程序运行前:

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

代码区:

  存放CPU执行的机器指令。

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

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

全局区:

  全局变量、全局常量、静态变量存放在此。

  (全局区的数据在程序结束后由操作系统释放)

复制代码
 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 //全局变量
 6 int g_a = 10;
 7 int g_b = 10;
 8 
 9 //全局常量
10 const int c_g_a = 10;
11 const int c_g_b = 10;
12 
13 //全局字符串变量
14 string g_s_a = "hello c++";
15 string g_s_b = "bye bye c++";
16 //全局字符串变量
17 const string c_g_s_a = "hello c++";
18 const string c_g_s_b = "bye bye c++";
19 
20 int main()
21 {
22     //局部变量
23     int a = 10;
24     int b = 10;
25 
26     //局部常量
27     const int c_l_a = 10;
28     const int c_l_b = 10;
29 
30     //局部字符串变量
31     string sa = "hello world";
32     string sb = "hello c++";
33 
34     //局部字符串常量
35     const string c_sa = "hello c++";
36     const string c_sb = "bye bye c++";
37 
38     //打印 局部变量、局部常量 的地址
39     cout << "局部变量a地址为: " << (int)&a << endl;
40     cout << "局部变量b地址为: " << (int)&b << endl;
41     cout << "局部常量c_l_a地址为: " << (int)&c_l_a << endl;
42     cout << "局部常量c_l_b地址为: " << (int)&c_l_b << endl;
43 
44     cout << "局部字符串变量地址为: " << (int)&sa << endl;
45     cout << "局部字符串变量地址为: " << (int)&sb << endl;
46 
47     cout << "局部字符串常量地址为: " << (int)&c_sa << endl;
48     cout << "局部字符串常量地址为: " << (int)&c_sb << endl;
49 
50     //静态变量
51     static int s_a = 10;
52     static int s_b = 10;
53 
54     
55 
56     
57 
58     //打印 静态变量、全局变量、全局常量、字符串变量的 地址
59     cout << "静态变量s_a地址为: " << (int)&s_a << endl;
60     cout << "静态变量s_b地址为: " << (int)&s_b << endl;
61 
62     cout << "全局变量g_a地址为: " << (int)&g_a << endl;
63     cout << "全局变量g_b地址为:" << (int)&g_b << endl;
64 
65 
66     cout << "全局常量c_g_a地址为: " << (int)&c_g_a << endl;
67     cout << "全局常量c_g_b地址为: " << (int)&c_g_b << endl;
68 
69     
70 
71     cout << "全局字符串变量地址为: " << (int)&g_s_a << endl;
72     cout << "全局字符串变量地址为: " << (int)&g_s_b << endl;
73 
74     cout << "全局字符串常量地址为: " << (int)&c_g_s_a << endl;
75     cout << "全局字符串常量地址为: " << (int)&c_g_s_b << endl;
76 
77 
78     system("pause");
79 
80     return 0;
81 }
复制代码

运行结果

 

 总结:

  C++中在程序运行前分为全局区和代码区

  代码区特点是共享和只读

  全局区中存放全局变量、全局常量、静态变量。

  局部区中存放局部变量、局部常量

 

1.2 程序运行后

栈区:

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

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

如:

复制代码
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int* func()
 6 {
 7     int a = 10;
 8     return &a;
 9 }
10 
11 int main()
12 {
13     int* p = func();
14 
15     cout << *p << endl;
16     cout << *p << endl;
17 
18     system("pause");
19 
20     return 0;
21 }
复制代码

运行结果:

 

 第一次输出*p时为正常结果,这是因为编译器考虑到函数func中局部变量a的地址可能还会用到,所以没有释放,第二次输出就是个随机数了。

 

堆区:

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

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

示例:

复制代码
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int* func()
 6 {
 7     int *a = new int(10);
 8     return a;
 9 }
10 
11 int main()
12 {
13     int* p = func();
14 
15     cout << *p << endl;
16     cout << *p << endl;
17 
18     system("pause");
19 
20     return 0;
21 }
复制代码

运行结果:

 

 

 总结:

  堆区数据由程序员管理开辟和释放

  堆区数据利用new关键字进行开辟内存

 

1.3 new操作符

  C++中利用 new 操作符在堆区开辟数据,堆区开辟的数据,由程序员手动开辟、手动释放,释放利用操作符 delete

【语法】[new 数据类型]

  利用new创建的数据,会返回该数据对应类型的指针。

示例1:基本语法

复制代码
#include <iostream>

using namespace std;

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

int main()
{
    int* p = func();

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

    //利用delete释放堆区数据
    delete p;
    //cout << *p << endl;    //报错,释放的空间不可访问

    system("pause");

    return 0;
}
复制代码

总结:释放的空间不可访问

 

示例2:开辟数组

复制代码
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int* arr = new int[10];
 8 
 9     for (int i = 0; i < 10; i++)
10     {
11         arr[i] = i + 100;
12     }
13 
14     for (int i = 0; i < 10; i++)
15     {
16         cout << arr[i] << endl;
17     }
18     
19     //释放数组delete后加[]
20     delete[] arr;
21 
22     system("pause");
23 
24     return 0;
25 }
复制代码

 

posted @   眉间  阅读(38)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示