首页 > 系统相关 >内存分区模型(代码区、全局区、栈区、堆区)

内存分区模型(代码区、全局区、栈区、堆区)

时间:2023-02-15 11:33:00浏览次数:42  
标签:栈区 10 释放 cout int 堆区 内存 new


从这开始,c++开始了进阶学习!!

c++程序执行时,将内存划分4个区域

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

内存四区的意义:

不同区域存放的数据,赋予不同的生命周期

文章目录

  • ​​1.1 程序执行前​​
  • ​​代码区:​​
  • ​​全局区:​​
  • ​​1.2 程序运行后​​
  • ​​栈区:​​
  • ​​堆区:​​
  • ​​1.3 new操作符​​

1.1 程序执行前

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

代码区:
  • 存放CPU执行的机器指令(二进制代码)
  • 代码区是共享的,共享的目的是对于频繁被执行的程序,只需要在内存中有一份代码即可
  • 代码区也是只读的,使其只读的原因是防止程序意外地修改了他的指令
全局区:
  • 全局变量和静态变量存放在此
  • 全局区还包含了常量区,字符串常量和其他常量也存放在此
  • 该区域的数据在程序结束后由操作系统释放
#include<iostream>
#include<string>
using namespace std;
//全局变量
int g_a = 10, g_b = 10;
//const修饰的全局变量
const int c_g_a = 10;

int main()
{
system("color 5E");
//局部变量
int a = 10, 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,属于静态变量
static int s_a = 10;
static int s_b = 10;
cout << "静态变量g_b的地址为:" << int(&s_a) << endl;
cout << "静态变量g_b的地址为:" << int(&s_b) << endl;
//字符串常量
cout << "字符串常量的地址为:" << int(&"helll" )<< endl;

//const修饰的变量
//const修饰的全局变量,修饰的局部变量
const int c_a = 10;
cout << "const修饰的全局变量:" << int(&c_g_a) << endl;
cout << "const修饰的局部变量:" << int(&c_a) << endl;
return 0;
}

内存分区模型(代码区、全局区、栈区、堆区)_堆区


内存分区模型(代码区、全局区、栈区、堆区)_堆区_02

1.2 程序运行后

栈区:

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

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

#include<iostream>
#include<string>
using namespace std;
int * func()
{
int a = 10;//局部变量,存放在栈区
return &a;//返回局部变量地址
}
int main()
{
system("color 5E");
int *p = func();//用指针接收函数的返回值
cout << *p << endl;//第一次打印正确数字,是因为编译器做了保留,防止误操作
cout << *p << endl;//第二次不会保留
return 0;
}

结果说明:(1)第一次打印正确数字,是因为编译器做了保留,防止误操作;(2)第二次不会保留

内存分区模型(代码区、全局区、栈区、堆区)_数据_03

堆区:

由程序员分配释放,若程序员不释放,程序结束后自动释放,C++中主要利用new在堆区开辟内存,然后把数据保存在堆区,指针地址还保存在栈区

#include<iostream>
#include<string>
using namespace std;

int * func()
{
//利用new关键字,可以把数据开辟到堆区
//指针本质也是局部变量,放在栈区,指针保存的数据放在堆区
int * p=new int(10);
return p;
}
int main()
{
system("color 5E");
int *p = func();
cout << *p << endl;
cout << *p << endl;
cout << *p << endl;
cout << *p << endl;
return 0;
}

内存分区模型(代码区、全局区、栈区、堆区)_c++_04

1.3 new操作符

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

语法:new 数据类型

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

#include<iostream>
#include<string>
using namespace std;

int * func()
{
//在堆区创建整型数据
//new返回该数据类型的指针
int * p=new int(100);
return p;
}
void test01()
{
int *p = func();
cout << *p << endl;
cout << *p << endl;
cout << *p << endl;
//堆区的数据由程序员开辟、释放
//若要释放堆区的数据
delete p;
//cout << *p << endl;//非法操作
}
//2、在堆区利用new开辟数组
void test02()
{
//创建整型的数组,在堆区
int *arr = new int[10];
for (int i = 0; i < 10; i++)
{
arr[i] = i;
}
for (int i = 0; i < 10; i++)
{
cout << arr[i] << " ";
}
//释放堆区数组
//释放数组的时候,加上[]
delete[] arr;
//cout << arr[1] << endl;//释放之后就不能再调用,调用会报错“非法操作”
}
int main()
{
system("color 5F");
test01();
test02();
return 0;
}

内存分区模型(代码区、全局区、栈区、堆区)_c++_05

内存分区模型(代码区、全局区、栈区、堆区)_数据_06


标签:栈区,10,释放,cout,int,堆区,内存,new
From: https://blog.51cto.com/u_14935708/6058985

相关文章