当前位置:网站首页>内存分区模型
内存分区模型
2022-06-26 16:29:00 【Meaauf】
内存分区模型
- 代码区:存放函数体的二进制代码,由操作系统进行管理的
- 全局区:存放全局变量和静态变量以及常量
- 栈区:由编译器自动分配,存放函数的参数值,局部变量等
- 堆区:由程序员分配和释放,若程序员不释放,程序结束时由操作系统回收
代码运行前
代码区
- 存放CPU执行的机器指令
- 代码区是共享的,共享的目的是对于频繁被执行的程序,只需要在内存中存在一分代码即可
- 代码区是只读的,目的是防止程序意外的修改指令
全局区
- 全局变量和静态变量存放在此
- 全局区还包含常量、字符串常量和其他常量也存放在此
- 该区域的数据在程序结束后由操作系统释放
#include <iostream>
using namespace std;
int global_a=10; // 全局变量
const int const_global_a=10; // const修饰的全局常量
int main(void)
{
int a=10; // 局部变量
const int const_a=10; // const修饰的局部常量
static int b=10; // 静态变量
cout << "Hello" << endl; // "Hello" 字符串常量
return 0;
}

总结
- C++在程序运行前分为全局区和代码区
- 代码区特点是共享和只读
- 全局区中存放全局变量、静态变量、常量
- 常量区中存放const修饰的全局变量和字符串常量
代码运行后
栈区
- 由编译器自动分配释放,存放函数的参数值、局部变量等
不要尝试返回局部变量的地址,栈区开辟的空间会有编译器自动释放
#include <iostream>
using namespace std;
int* f()
{
int a=10;
return &a;
}
int main(void)
{
int*p=f(); //尝试接受函数的局部变量
cout << *p << endl;
cout << *p << endl;
cout << *p << endl;
return 0;
}

堆区
new分配空间
int * arr=new int[10]; // 创建10整形数据的数组
delete[] arr; // 释放堆区数组
int * a=new int(10); // 创建值为10的整形变量
delete(a);
- 由程序员分配释放,若程序员不释放,程序结束时由操作系统回收
- 通过new在堆区开辟空间(相当于C中的malloc)
#include <iostream>
using namespace std;
int* f(int n)
{
return new int(n); // 分配空间 n:生成的int初始值
}
int main(void)
{
int * p=f(10);
cout << *p << endl;
cout << *p << endl;
cout << *p << endl; // 如果不手动释放,值会一直存在
delete(p); // 释放堆区开辟的空间
cout << *p << endl;
cout << *p << endl;
cout << *p << endl;
return 0;
}

边栏推荐
- Develop operator based on kubebuilder (for getting started)
- Exquisite makeup has become the "soft power" of camping, and the sales of vipshop outdoor beauty and skin care products have surged
- [从零开始学习FPGA编程-46]:视野篇 - 集成电路的发展与技术进步
- JS教程之Electron.js设计强大的多平台桌面应用程序的好工具
- 2 three modeling methods
- Solution for filtering by special string of microservice
- [207] several possible causes of Apache crash
- Practice of federal learning in Tencent micro vision advertising
- When a programmer is disturbed 10 times a day, the consequences are amazing!
- TCP congestion control details | 1 summary
猜你喜欢

长安链交易防重之布谷鸟过滤器

国内首款开源 MySQL HTAP 数据库即将发布,三大看点提前告知

100+ data science interview questions and answers Summary - basic knowledge and data analysis

当一个程序员一天被打扰 10 次,后果很惊人!
Practice of federal learning in Tencent micro vision advertising

Arduino uno + DS1302 simple time acquisition and serial port printing

用Attention和微调BERT进行自然语言推断-PyTorch

首例猪心移植细节全面披露:患者体内发现人类疱疹病毒,死后心脏重量翻倍,心肌细胞纤维化丨团队最新论文...

100+数据科学面试问题和答案总结 - 基础知识和数据分析

This year, the AI score of college entrance examination English is 134. The research of Fudan Wuda alumni is interesting
随机推荐
1-12Vmware新增SSH功能
Acid of redis
R language generalized linear model function GLM, GLM function to build logistic regression model, analyze whether the model is over discrete, and use the ratio of residual deviation and residual degr
若依如何实现接口限流?
最小二乘系统辨识课 中篇:递归最小二乘
Develop operator based on kubebuilder (for getting started)
【力扣刷题】11.盛最多水的容器//42.接雨水
Net based on girdview control to delete and edit row data
JS教程之使用 ElectronJS、VueJS、SQLite 和 Sequelize ORM 从 A 到 Z 创建多对多 CRUD 应用程序
Ten thousand words! In depth analysis of the development trend of multi-party data collaborative application and privacy computing under the data security law
I regard it as a dry product with a monthly income of more than 30000 yuan for sidelines and more than 10000 yuan for novices!
基于STM32+华为云IOT设计的云平台监控系统
Structure the graduation project of actual combat camp
长安链交易防重之布谷鸟过滤器
Stm32h7b0 replaces the h750 program, causing the MCU to hang up and unable to burn the program
[untitled]
LeetCode 单周赛298,前三题
Tencent Peking University's sparse large model training acceleration program het was selected into the VLDB of the international summit
TCP congestion control details | 1 summary
day10每日3题(1):逐步求和得到正数的最小值