C++核心编程<内存分区模型>
- 1.内存分区模型
 - 1.1内存分区模型概述
 - 1.2内存分区的意义
 - 1.3程序允许前
 - 1.4程序运行后
 - 1.5new操作符
 
 
 
1.内存分区模型
 
1.1内存分区模型概述
 
- C++程序在执行时,将内存大方向分为4个区域 
- 代码区: 存放函数体的二进制代码,由操作系统进行管理的
 - 全局区: 存放全局变量和静态变量以及常量
 - 栈区: 由编译器自动分配释放,存放函数的参数值、局部变量等
 - 堆区: 由程序员分配和释放,若程序员不释放,程序结束时由操作系统回收
 
  
 
1.2内存分区的意义
 
 
1.3程序允许前
 
- 在程序编译后,生成看exe可执行文件,未执行该程序前分为两个区域
 
 
1.3.1代码区
 
- 存放CPU执行的机器指令
 - 代码区是共享的,共享的目的是对于频繁被执行的程序,只需要在内存中有一份代码即可
 - 代码区是只读的,使其只读的原因使防止程序意外地修改了它的指令
 
 
1.3.2全局区
 
- 全局变量和静态变量存放在此
 - 全局区还包含了常量区,字符串常量和**其他常量(const修饰的全局变量)**也存放在此
 - 该区域的数据在程序结束后由操作系统释放
 
 
1.3.2.1全局区的演示
 
#include<iostream>
using namespace std;
int gNum = 123;const int cgNum = 123;int main()
{static int sNum = 123;const string cstr = "富甲一方钱七";int num = 123;cout << "num的局部变量内存地址为: " << &num << endl;cout << "gNum全局变量的内存地址为: " << &gNum << endl;cout << "sNum静态变量的内存地址为: " << &sNum << endl;cout << "字符串常量的地址为: " << &"法外狂徒张三" << endl;cout << "const修饰的全局变量的地址为: " << &cgNum << endl;cout << "const修饰的局部变量地址为: " << &cstr << endl; system("pause");return 0;
}
 
1.4程序运行后
 
1.4.1栈区
 
- 由编译器自动分配释放,存放函数的参数值、局部变量等
 - 注意事项: 不要返回局部变量的地址,栈区开辟的数据由编译器自动释放
 
 
1.4.1.1栈区演示
 
#include<iostream>
using namespace std;
int* func();int main()
{int* p = func();cout << *p << endl; cout << *p << endl; system("pause");return 0;
}int* func() 
{int num = 23; return #}
 
1.4.2堆区
 
- 由程序员分配释放,若程序员不释放,程序结束时,由操作系统回收
 - 在c++中主要利用new在堆区开辟内存
 
 
1.4.2.1堆区演示
 
#include<iostream>
using namespace std;int* func();int main()
{int* p = func();cout << *p << endl; cout << *p << endl; system("pause");return 0;
}int* func() 
{int* p = new int(123);return p;
}
 
1.5new操作符
 
1.5.1new操作的概述
 
- C++利用new操作符在堆区开辟数据
 - 堆区开辟的数据,由程序员手动开辟,手动释放,释放利用操作符delete
#include<iostream>
using namespace std;int* func();int main()
{int* p = func();cout << *p << endl; cout << *p << endl; system("pause");return 0;
}int* func() 
{int* p = new int(123);return p;
}
  - 语法
new 数据类型
  - 利用new创建的数据,会返回该数据对应的类型的指针
#include<iostream>
using namespace std;
int* func();
void test_1();
void teste_2();int main()
{test_1();system("pause");return 0;
}
int* func() 
{int* p = new int(123);return p;
}
void test_1() 
{int* p = func();cout << *p << endl; delete p;
}
void test_2()
{int* arr = new int[10];delete[] arr;
}