当前位置:网站首页>【C语言】程序的内存四区模型
【C语言】程序的内存四区模型
2022-06-12 03:43:00 【嵌入式职场】
目录
1、程序的内存四区模型流程

- 操作系统把物理硬盘代码load到内存
操作系统把c代码分成四个区
操作系统找到main函数入口执行
2、堆区(heap)
一般由程序员分配释放(动态内存的释放与申请),若程序员不释放,程序结束时候,可能由操作系统回收。
首先应该知道操作系统有一个记录空闲内存地址的链表,当系统收到程序的申请时,会遍历该表,寻找第一个空间大于所申请空间的堆结点,然后将该结点从空闲结点链表中删除,并将该结点的空间分配给程序,另外,对于大多数系统,会在这块内存空间中的首地址处记录本次分配的大小,这样,代码中的delete语句才能正确的释放本内存空间。另外,由于找到的堆结点的大小不一定正好等于申请的大小,系统会自动的将多余的那部分重新放入空闲链表中。
堆是由new分配的内存,一般速度比较慢,而且容易产生内存碎片,不过用起来最方便.
需要程序员自己分配,如:
ch = (char *)malloc(20); //分配的20字节区域就是堆区
在C++中:
p = new char[10];
3、栈区(stack)
由编译器自动分配释放,存放函数参数值,局部变量值;
只要栈的剩余空间大于所申请空间,系统将为程序提供内存,否则将报异常提示栈溢出。
栈由系统自动分配,速度较快。但程序员是无法控制的。
例如:
声明函数里面的局部变量:int m; 系统在栈中为其开辟空间。
4、全局区(global)或static(静态区)
全局变量与静态变量的存储是放在一起的,初始化的全局变量与静态变量在一块区域。
未初始化的全局变量与未初始化的静态变量在相邻的另一块区域。
该区域在程序运行结束后由操作系统回收。
5、代码区(code)
存放函数体的二进制代码;
6、具体实例
#include <stdio.h>
#include <stdlib.h>
int a = 4; //全局初始化区
char *ch; //全局未初始化区
int main() {
int m; //栈
char *p; //栈
char *p3 = "123"; // 123/0在常量区,p3在栈上
static int n = 12; //全局初始化区
p = (char *)malloc(sizeof(char)); //分配的字节区域就是堆区
ch = (char *)malloc(20); //分配的20字节区域就是堆区
return 0;
}边栏推荐
- 双目标定学习资料整理
- 2020-12-07
- Computer configuration suggestions for learning modeling
- Drop down menu dropdown yyds dry inventory of semantic UI
- 【鸿蒙】 使用定时器做一个简单的抢红包小游戏
- 绝对定位使盒子居中显示的三种方式
- go 递归无限极分类
- [Business Research Report] the salary growth rate report of each industry in 2022 includes regional growth rate - download link is attached
- The road of global evolution of vivo global mall -- multilingual solution
- 2022 communication industry ultimate Exhibition Guide
猜你喜欢
![[C language] dynamic memory allocation](/img/ac/ba3882c76f93dde0ab0d5294c5b6d9.jpg)
[C language] dynamic memory allocation

Drop down menu dropdown yyds dry inventory of semantic UI

MySQL的check约束数字问题

Mongodb essence summary
![[Business Research Report] the salary growth rate report of each industry in 2022 includes regional growth rate - download link is attached](/img/11/291f858ce2444f2fa96c89f7454c17.jpg)
[Business Research Report] the salary growth rate report of each industry in 2022 includes regional growth rate - download link is attached

怎样拥有心灵的平和?获得一颗全新的心灵

顺序表与链表-----进阶

What is the core of Web3?

Absolute positioning three ways to center the box

技术经济与企业管理 复习 第四章
随机推荐
The rise of another domestic mobile phone chip is close to the height reached by Huawei
VIM command Encyclopedia
2020-12-10
Yyds dry inventory MySQL learning - how transactions are isolated
Special materials | household appliances, white electricity, kitchen electricity
怎样拥有心灵的平和?获得一颗全新的心灵
顺序表与链表-----进阶
The road of global evolution of vivo global mall -- multilingual solution
Final summary of addition, deletion, modification and query
R语言plotly可视化:使用plotly可视化简单线性回归模型的回归线(simple regression model linear regression plots)
(idea)the file size(2.85M) exceeds configured limit(2.56M). Code insight features are not available
CA证书及密钥对应用笔记
成功解决:TypeError: the JSON object must be str, bytes or bytearray, not dict
Simple database connection example
Network tester operation manual renix rack management
2020-12-06
R language plot visualization: use plot to visualize simple regression model linear regression plots
Key points of code neatness (III)
Convert py file to EXE file
Mongodb essence summary