当前位置:网站首页>【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;
}边栏推荐
- Drop down menu dropdown yyds dry inventory of semantic UI
- Lighting Basics: optical model
- C language array
- Page crash handling method
- Mosaïque d'images basée sur la matrice de transformation
- How to modify the result name of MySQL query result 1 and result 2
- Sed command
- webpack---优化_缓存
- What is the core of Web3?
- CA证书及密钥对应用笔记
猜你喜欢

2022 communication industry ultimate Exhibition Guide

Sequence list and linked list ----- advanced

Introduce the functions of the new project aleo
![[data recovery in North Asia] data recovery in which the logical volume of the server is changed and the file system is damaged due to system reinstallation](/img/7f/ad628a45bec0ac9a5586d743ca0310.jpg)
[data recovery in North Asia] data recovery in which the logical volume of the server is changed and the file system is damaged due to system reinstallation
![[MySQL] MySQL installation](/img/f0/2b65b71e79ea0cc0790e4848092374.png)
[MySQL] MySQL installation

Laravel 8 selects JWT for interface verification

Steamvr--- grab objects

Absolute positioning three ways to center the box

1187_ C language implementation of hysteresis processing
![[Business Research Report] 2021 global mobile game player white paper - download link attached](/img/6f/4425ead27fc3cf117f6756502477cf.jpg)
[Business Research Report] 2021 global mobile game player white paper - download link attached
随机推荐
vim命令大全
MySQL create user and authorize
19.tornado项目之优化数据库查询
AI interview bag | Netease mutual entertainment AI Lab artificial intelligence research engineers share on both sides
双目标定学习资料整理
Mosaïque d'images basée sur la matrice de transformation
2020-12-12
消息队列概述
R语言plotly可视化:使用plotly可视化简单线性回归模型的回归线(simple regression model linear regression plots)
1186_ Accumulation of embedded hardware knowledge_ Triode and three electrodes
PostgreSQL basic introduction and deployment
Plot visualization in R language: visualize the scatter diagram of the actual value and the predicted value of the regression model, analyze the prediction efficiency of the regression model, distingu
【mysql】mysql安装
Evolution and practice of Unicom real-time computing platform
Fastjson enables safemode, closes autotype, and removes security vulnerabilities
Implementation of fitness club management system based on SSH
webpack---优化_缓存
What is the difference between the gin framework of golang and the various methods of receiving parameters and various bindings?
[C language] dynamic memory allocation
魏武帝 太祖知不可匡正,遂不复献言