1. 内存分区模型
c++程序执行时,内存大方向划分为4个区域
代码区:存放函数体的二进制代码,由操作系统进行管理
全局区:存放全局变量和静态变量以及常量
栈区:由编译器自动分配释放,存放函数的参数值,局部变量等
堆区:由程序员分配和释放,若程序员不释放,程序结束时由操作系统回收
内存四区意义:
不同区域存放的数据,赋予不同的生命周期,给我们更大的灵活编程
1.1 程序运行前
在程序编译后,生成了exe可执行程序,未执行程序前分为两个区域
代码区:
存放cpu执行的机器指令
代码区是共享的,目的是为了对于频繁被执行的程序,只需要在内存中有一份代码即可
代码区是只读的,原因是防止程序意外修改了它的指令
全局区:
全局变量和静态变量存放于此
全局区还包含了常量区,字符串常量和其他常量(const)也存放在此
该区域的数据在程序结束后由操作系统释放
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm> //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
#include<ctime>
#include<stack>
#include<queue>
//全局变量
int g_a = 10;
int g_b = 10;
//const修饰的全局变量(也叫全局常量)
const int g_c = 10;
const int g_d = 10;
int main(int argc,char**argv) { //没有写到函数体中的变量就是全局变量
//创建普通局部变量
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;
//const修饰的变量
//const修饰的全局变量
cout << "全局常量g_c的地址为:" << (int)&g_c << endl;
cout << "全局常量g_d的地址为:" << (int)&g_d << endl;
//const修饰的局部变量
const int g_e = 10;
const int g_f = 10;
cout << "局部常量g_e的地址为:" << (int)&g_e << endl;
cout << "局部常量g_f的地址为:" << (int)&g_f << endl;
system("pause");
return 0;
}
1.2 程序运行后
栈区:
由编译器自动分配释放,存放函数的参数值,局部变量等
注意事项:不要返回局部变量的地址,栈区开辟的数据由编译器自动释放
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm> //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
#include<ctime>
#include<stack>
#include<queue>
int* func() { //形参数据也会存放在栈区(这行代码里没有放比如int b这样的形参)
int a = 10; //局部变量,存放在栈区,栈区的数据在执行完后自动释放
return &a; //返回局部变量的地址
}
int main(int argc,char**argv) {
//接受func函数的返回值
int* p = func();
cout << *p << endl; //10 第一次可以打印正确是因为编译器做了保留
cout << *p << endl; //20458156
system("pause");
return 0;
}
堆区:
由程序员分配释放,若程序员不释放,程序结束时会被操作系统回收
在c++中主要利用new在堆区开辟内存
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm> //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
#include<ctime>
#include<stack>
#include<queue>
int* func() {
//利用new关键字 可以将数据开辟到堆区
//指针本质也是局部变量,放在栈上,指针保存的数据是放在堆区
int *p = new int(10);
return p;
}
int main(int argc,char**argv) {
//在堆区开辟数据
int* p = func();
cout << *p << endl;
system("pause");
return 0;
}
1.3 new操作符
c++中利用new操作符在堆区开辟数据
堆区开辟的数据,由程序员手动开辟,手动释放,释放用操作符delete
利用new创建的数据,会返回该数据对应的类型的指针
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm> //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
#include<ctime>
#include<stack>
#include<queue>
int* func() {
int *p = new int(10);
return p;
}
void test01() {
int* p = func();
cout << *p << endl;
delete p;
//cout << *p << endl; 内存已经被释放 再次运行就是非法操作!
}
//在堆区开辟数组
void test02() {
int* arr = new int[10];
for (int i = 0; i < 10; i++) {
arr[i] = i + 100;
}
for (int i = 0; i < 10; i++) {
cout << arr[i] << endl;
}
//释放堆区数组
delete[] arr;
}
int main(int argc,char**argv) {
//test01();
test02();
system("pause");
return 0;
}