一,C/C++的内存分布
1.1C/C++内存分布
在C语言阶段学习的时候,总会有一些问题就是我们写过的各种各样的代码,局部变量,全局变量,静态变量等它们到底是存在哪的呢?相信有很多人在学C/C++的时候会有这些疑问,下面就来看看C/C++中的内存分布:
代码语言:javascript
AI代码解释
int globalVar = 1; static int staticGlobalVar = 1; void Test() { static int staticVar = 1; int localVar = 1; int num1[10] = { 1, 2, 3, 4 }; char char2[] = "abcd"; const char* pChar3 = "abcd"; int* ptr1 = (int*)malloc(sizeof(int) * 4); int* ptr2 = (int*)calloc(4, sizeof(int)); int* ptr3 = (int*)realloc(ptr2, sizeof(int) * 4); free(ptr1); free(ptr3); }上面代码中的一些局部变量、全局变量、静态变量、数组、指针等都存在什么地方?
说明:
- 栈又叫堆栈:存储非静态局部变量/函数参数/返回值等等,栈是向下增长的。
- 内存映射段:是高效的I/O映射方式,用于装载一个共享的动态内存库。用户可使用系统接口创建共享共享内存,做进程间通信.
- 堆:用于程序运行时动态内存分配,堆是可以上增长的。
- 数据段:存储全局数据和静态数据。
- 代码段:可执行的代码/只读常量。
1.2函数栈帧的创建和销毁
上面我们看到的大部分在函数内部创建的变量,指针,数组,它们基本都是在栈区创建的说明栈区对我们来说也是一块比较重要的区域,那么为什么它们是存在栈区的呢?
- 内存管理的高效性:栈区采用后进先出(LIFO)的分配机制,内存分配和释放仅需移动栈指针,速度极快。函数调用时,栈指针下移分配空间;函数返回时,栈指针上移自动回收内存,无需复杂的内存管理操作。
- 生命周期与函数调用匹配:局部变量的生命周期严格绑定函数执行周期。栈区的自动分配和释放特性完美契合这一需求,避免了手动管理内存的复杂性,减少内存泄漏风险。 下面我们就简单来看看函数栈帧的创建和销毁:
在这里插入图片描述
下面以一个简单的
main函数调用Add函数为例来看看,函数栈帧创建和销毁的具体过程:
在这里插入图片描述
二,C语言中的动态内存管理方式
在C语言中,内存管理是通过我们之前所学过的三个内存函数,
malloc calloc realloc来管理的,比如下面这段代码:
www.dongchedi.com/article/7592326493200482841
www.dongchedi.com/article/7592329049750995481
www.dongchedi.com/article/7592327070701961753
www.dongchedi.com/article/7592322906135413273
www.dongchedi.com/article/7592308231180026392
www.dongchedi.com/article/7592331190716498457
www.dongchedi.com/article/7592320610865676825
www.dongchedi.com/article/7592326493201007129
www.dongchedi.com/article/7592322714795688510
www.dongchedi.com/article/7592327392942178841
www.dongchedi.com/article/7592319617927283225
www.dongchedi.com/article/7592327476282638873
www.dongchedi.com/article/7592319094112846360
www.dongchedi.com/article/7592326854393152025
www.dongchedi.com/article/7592319386741244441
www.dongchedi.com/article/7592321316729504281
www.dongchedi.com/article/7592319724022039102
www.dongchedi.com/article/7592327140348379672
www.dongchedi.com/article/7592319412238418457
www.dongchedi.com/article/7592319724022137406
www.dongchedi.com/article/7592319070578917950
www.dongchedi.com/article/7592317677180486168
www.dongchedi.com/article/7592331430680953368
www.dongchedi.com/article/7592317943112057368
www.dongchedi.com/article/7592312482736685593
www.dongchedi.com/article/7592326787452011070
www.dongchedi.com/article/7592320171805934142
www.dongchedi.com/article/7592312175293891097
www.dongchedi.com/article/7592326095602221593
www.dongchedi.com/article/7592313460353466942
www.dongchedi.com/article/7592315921234985497
www.dongchedi.com/article/7592318584517493310
www.dongchedi.com/article/7592313903104164377
www.dongchedi.com/article/7592314981853905432
www.dongchedi.com/article/7592327419139654169
www.dongchedi.com/article/7592312482736620057
www.dongchedi.com/article/7592327392941589017
www.dongchedi.com/article/7592313382368297497
www.dongchedi.com/article/7592317900531548734
www.dongchedi.com/article/7592315372943327769
www.dongchedi.com/article/7592311873639432729
www.dongchedi.com/article/7592316979160121918
www.dongchedi.com/article/7592312719127511577
www.dongchedi.com/article/7592316786557616664
www.dongchedi.com/article/7592327116290032153
www.dongchedi.com/article/7592326416835002904
www.dongchedi.com/article/7592313347400270360