当前位置:网站首页>Memory partition model
Memory partition model
2022-06-26 16:35:00 【Meaauf】
Memory partition model
- Code section : Store the binary code of the function body , Managed by the operating system
- Global area : Store global and static variables and constants
- The stack area : Automatically assigned by the compiler , Stores the parameter values of the function , Local variables, etc
- Heap area : Assigned and released by the programmer , If programmers don't release , Recycle by the operating system at the end of the program
Before the code runs
Code section
- Deposit CPU Machine instructions executed
- The code area is shared , The purpose of sharing is for programs that are frequently executed , You only need a bit of code in memory
- The code area is read-only , The purpose is to prevent the program from accidentally modifying instructions
Global area
- Global variables and static variables are stored here
- The global area also contains constants 、 String constants and other constants are also stored here
- The data in this area is released by the operating system at the end of the program
#include <iostream>
using namespace std;
int global_a=10; // Global variables
const int const_global_a=10; // const Modified global constant
int main(void)
{
int a=10; // local variable
const int const_a=10; // const Modified local constants
static int b=10; // Static variables
cout << "Hello" << endl; // "Hello" String constant
return 0;
}

summary
- C++ Before the program runs, it is divided into global area and code area
- Code areas are characterized by shared and read-only
- Global variables are stored in the global area 、 Static variables 、 Constant
- In the constant area const Modified global variables and string constants
After the code runs
The stack area
- Release is automatically allocated by the compiler , Stores the parameter values of the function 、 Local variables, etc
Do not try to return the address of a local variable , The space opened up by the stack area will be automatically released by the compiler
#include <iostream>
using namespace std;
int* f()
{
int a=10;
return &a;
}
int main(void)
{
int*p=f(); // Try to accept a local variable of a function
cout << *p << endl;
cout << *p << endl;
cout << *p << endl;
return 0;
}

Heap area
new Allocate space
int * arr=new int[10]; // establish 10 An array of shaped data
delete[] arr; // Free heap array
int * a=new int(10); // Create value for 10 The shaping variable of
delete(a);
- Release is assigned by the programmer , If programmers don't release , Recycle by the operating system at the end of the program
- adopt new Open up space in the stack area ( amount to C Medium malloc)
#include <iostream>
using namespace std;
int* f(int n)
{
return new int(n); // Allocate space n: Generated int Initial value
}
int main(void)
{
int * p=f(10);
cout << *p << endl;
cout << *p << endl;
cout << *p << endl; // If you don't release it manually , The value will always exist
delete(p); // Free up space created by heap area
cout << *p << endl;
cout << *p << endl;
cout << *p << endl;
return 0;
}

边栏推荐
- proxy
- Which position does Anxin securities rank? Is it safe to open an account?
- 【毕业季】致毕业生的一句话:天高任鸟飞,海阔凭鱼跃
- 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
- 100+数据科学面试问题和答案总结 - 基础知识和数据分析
- How can I get the stock account opening discount link? Is online account opening safe?
- Hyperf框架使用阿里云OSS上传失败
- 国内首款开源 MySQL HTAP 数据库即将发布,三大看点提前告知
- 精致妆容成露营“软实力”,唯品会户外美妆护肤产品销量激增
- How to separate jar packages and resource files according to packaging?
猜你喜欢

精致妆容成露营“软实力”,唯品会户外美妆护肤产品销量激增

Arduino UNO + DS1302简单获取时间并串口打印

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

对话长安马自达高层,全新产品将在Q4发布,空间与智能领跑日系

Natural language inference with attention and fine tuning Bert pytorch

无需人工先验!港大&同济&LunarAI&旷视提出基于语义分组的自监督视觉表征学习,显著提升目标检测、实例分割和语义分割任务!...

SAP OData 开发教程 - 从入门到提高(包含 SEGW, RAP 和 CDP)
Scala Foundation (2): variables et types de données

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

Unlock the value of data fusion! Tencent angel powerfl won the "leading scientific and Technological Achievement Award" at the 2021 digital Expo
随机推荐
Redis order sorting command
Keepalived 实现 Redis AutoFailover (RedisHA)
Redis顺序排序命令
请指教同花顺软件究竟是什么?网上开户是否安全么?
Supplement the short board - Open Source im project openim about initialization / login / friend interface document introduction
Several forms of buffer in circuit
Binary array command of redis
1-12vmware adds SSH function
Redis migration (recommended operation process) 1
Notes on key review of software engineering at the end of the term
Pybullet robot simulation environment construction 5 Robot pose visualization
Niuke programming problem -- dynamic programming of must brush 101 (a thorough understanding of dynamic programming)
Redis 迁移(操作流程建议)
JS教程之 使用 Electron.JS 构建原生桌面应用程序乒乓游戏
Kept to implement redis autofailover (redisha)
IAR engineering adapts gd32 chip
Detailed explanation of cookies and sessions
知道这几个命令让你掌握Shell自带工具
了解下常见的函数式接口
[从零开始学习FPGA编程-46]:视野篇 - 集成电路的发展与技术进步