当前位置:网站首页>【嵌入式】---- 内存四区介绍
【嵌入式】---- 内存四区介绍
2022-07-03 11:40:00 【伊 娃】
一、内存四区由来
程序执行是跑在内存中的,而程序中不同的对象(如 指针,局部变量,全局变量,函数参数 等等)要求的生命周期是不同的,所以将这些对象放在不同的区段,这样就赋予了这些对象不同的生命周期,内存四区由此而来。
二、内存四区介绍
内存四区分为:栈区,堆区,全局区,代码区。
1. 栈区(stack)
栈区由编译器自动分配和释放。
栈区主要存放函数参数、函数返回值、局部变量。
2. 堆区(heap)
堆区由程序员手动分配和释放。如果程序员未释放,在程序运行结束时,由系统释放。
堆区主要存放由 malloc 关键字申请的内存空间。在C++语言中关键字 new 申请的内存也在堆区。
3. 全局区
全局区又叫数据区或者静态区。
全局区分为三段: data段 、bss段、rodata 段。
(1).data 段存放已初始化和值不为0的全局变量和静态变量
(2).bss 段存放未初始化和初始化值为0的全局变量和静态变量。
(3).rodata 段存放各类常量,如:const、字符串、#define等
data 段又叫 RW-data 段
bss 段又叫 ZI-data 段
rodata 段又叫常量区或只读存储区
4. 代码区(text)
代码区又叫 code 段 或 text 段。
用来存放程序代码,由操作系统进行管理,只读。
三、额外补充
flash 或者 ROM 掉电后数据还是保存的,保存的数据有 RO-data、RW-data、代码区
RAM 一般指的就是 sdram,运行速度快,但是掉电数据会丢失,保存的数据有 栈区、堆区、数据区
注意:
1,flash 或者 ROM 中为什么不保存 ZI-data?
ZI-data 段数据未初始化或初始化为0,在启动系统时直接将这块 flash 或者 ROM 清0即可,无需保存。
2,RW-data 是 RAM 使用的,为什么存储到 flash 或 ROM 中?
RW-data 这部分变量已经初始化了,只存在RAM中掉电会丢失,所以需要在flash 或 ROM 中存储一份
边栏推荐
猜你喜欢
OpenGL index cache object EBO and lineweight mode
OpenGL draws colored triangles
LeetCode 0556.下一个更大元素 III - 4步讲完
Integer int compare size
OPenGL 基本知识(根据自己理解整理)
Ripper of vulnhub
QT OpenGL texture map
Php Export word method (One MHT)
Basic knowledge of OpenGL (sort it out according to your own understanding)
Unity3d learning notes 5 - create sub mesh
随机推荐
Ripper of vulnhub
OpenGL shader use
Fluent: Engine Architecture
Basic knowledge of OpenGL (sort it out according to your own understanding)
Differences between MySQL Union and union all
DEJA_ Vu3d - 054 of cesium feature set - simulate the whole process of rocket launch
Niuniu's team competition
[learning notes] DP status and transfer
PHP get the file list and folder list under the folder
[MySQL special] read lock and write lock
安裝electron失敗的解决辦法
vulnhub之momentum
Dart: about Libraries
How to deploy web pages to Alibaba cloud
Redis notes 01: Introduction
Wrong arrangement (lottery, email)
232. Implement queue with stack
为什么我的mysql容器启动不了呢
Pki/ca and digital certificate
ES6新特性