当前位置:网站首页>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;
}
边栏推荐
- Scala Foundation (2): variables et types de données
- JS教程之使用 ElectronJS 桌面应用程序打印贴纸/标签
- Solution for filtering by special string of microservice
- JS tutorial electron JS is a good tool for designing powerful multi platform desktop applications
- Big talk Domain Driven Design -- presentation layer and others
- [from database deletion to running] JDBC conclusion (finish the series in one day!! run as soon as you finish learning!)
- 网页课程设计大作业——华山旅游网
- Gui+sqlserver examination system
- 国内首款开源 MySQL HTAP 数据库即将发布,三大看点提前告知
- 【毕业季】致毕业生的一句话:天高任鸟飞,海阔凭鱼跃
猜你喜欢
1-12vmware adds SSH function
pybullet机器人仿真环境搭建 5.机器人位姿可视化
【毕业季】致毕业生的一句话:天高任鸟飞,海阔凭鱼跃
Dialogue with the senior management of Chang'an Mazda, new products will be released in Q4, and space and intelligence will lead the Japanese system
What does the inner structure of the neural network "alchemy furnace" look like? An interpretation of the thesis by the doctor of Oxford University
【力扣刷题】11.盛最多水的容器//42.接雨水
心情不好,我就这样写代码
Arduino uno + DS1302 simple time acquisition and serial port printing
5G未平6G再启,中国引领无线通信,6G的最大优势在哪里?
当一个程序员一天被打扰 10 次,后果很惊人!
随机推荐
了解下常见的函数式接口
r329(MAIX-II-A(M2A)资料汇总
【力扣刷题】11.盛最多水的容器//42.接雨水
网页课程设计大作业——华山旅游网
C# 读写文件从用户态切到内核态,到底是个什么流程?
Tencent Peking University's sparse large model training acceleration program het was selected into the VLDB of the international summit
R language plotly visualization: Violin graph, multi category variable violin graph, grouped violin graph, split grouped violin graph, two groups of data in each violin graph, each group accounts for
基於Kubebuilder開發Operator(入門使用)
Redis顺序排序命令
[chat in 5] eight years after graduation, I have been pursuing my dream
若依微服务特殊字符串被过滤的解决办法
Développer un opérateur basé sur kubebuilder (démarrer)
[Li Kou brush question] monotone stack: 84 The largest rectangle in the histogram
Redis 迁移(操作流程建议)
MHA switching (recommended operation process)
[understanding of opportunity -31]: Guiguzi - Daoyu [x ī] Crisis is the coexistence of danger and opportunity
Codeforces Round #802 (Div. 2)
JS教程之 使用 Electron.JS 构建原生桌面应用程序乒乓游戏
R语言plotly可视化:plotly可视化归一化的直方图(historgram)并在直方图中添加密度曲线kde、并在直方图的底部边缘使用geom_rug函数添加边缘轴须图
Niuke programming problem -- dynamic programming of must brush 101 (a thorough understanding of dynamic programming)