当前位置:网站首页>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;
}

边栏推荐
- 安信证券排名第几位?开户安全吗?
- Stm32h7b0 replaces the h750 program, causing the MCU to hang up and unable to burn the program
- 100+数据科学面试问题和答案总结 - 基础知识和数据分析
- [Blue Bridge Cup training 100 questions] scratch distinguishing prime numbers and composite numbers Blue Bridge Cup scratch competition special prediction programming question intensive training simul
- [机缘参悟-31]:鬼谷子-抵巇[xī]篇-危机是危险与机会并存
- 100+ data science interview questions and answers Summary - basic knowledge and data analysis
- [Li Kou brush question] monotone stack: 84 The largest rectangle in the histogram
- 架构实战营毕业设计
- The first open source MySQL HTAP database in China will be released soon, and the three highlights will be notified in advance
- "C language" question set of ⑩
猜你喜欢

Unlock the value of data fusion! Tencent angel powerfl won the "leading scientific and Technological Achievement Award" at the 2021 digital Expo

油田勘探问题
Redis Guide (8): principle and implementation of Qianfan Jingfa distributed lock

MS|谢黎炜组发现混合益生菌制剂及其代谢产物可缓解结肠炎

This year, the AI score of college entrance examination English is 134. The research of Fudan Wuda alumni is interesting

心情不好,我就这样写代码

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

Cloud platform monitoring system based on stm32+ Huawei cloud IOT design

Supplement the short board - Open Source im project openim about initialization / login / friend interface document introduction

Arduino uno + DS1302 simple time acquisition and serial port printing
随机推荐
Scala Foundation (2): variables et types de données
Least squares system identification class II: recursive least squares
若依微服务特殊字符串被过滤的解决办法
R语言广义线性模型函数GLM、glm函数构建逻辑回归模型(Logistic regression)、分析模型是否过离散(Overdispersion)、使用残差偏差与二项式模型中的剩余自由度的比率评估
IAR engineering adapts gd32 chip
Unlock the value of data fusion! Tencent angel powerfl won the "leading scientific and Technological Achievement Award" at the 2021 digital Expo
Oilfield exploration problems
【从删库到跑路】JDBC 完结篇(一天学完系列!!学完赶紧跑!)
Which position does Anxin securities rank? Is it safe to open an account?
stm32h7b0替代h750程序导致单片机挂掉无法烧录程序问题
Research on natural transition dubbing processing scheme based on MATLAB
无需人工先验!港大&同济&LunarAI&旷视提出基于语义分组的自监督视觉表征学习,显著提升目标检测、实例分割和语义分割任务!...
Natural language inference with attention and fine tuning Bert pytorch
R language plot visualization: plot visualizes the normalized histogram, adds the density curve KDE to the histogram, and uses geom at the bottom edge of the histogram_ Adding edge whisker graph with
Solidus labs welcomes zhaojiali, former head of financial innovation in Hong Kong, as a strategic adviser
心情不好,我就这样写代码
Exquisite makeup has become the "soft power" of camping, and the sales of vipshop outdoor beauty and skin care products have surged
Qt 5.9.8 安装教程
C# 读写文件从用户态切到内核态,到底是个什么流程?
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