2026/2/18 15:07:54
网站建设
项目流程
北京网站建设推广服,怎么样做网站优化,个人如何建立微信公众号,企业管理咨询服务内容一#xff0c;C/C的内存分布1.1C/C内存分布在C语言阶段学习的时候#xff0c;总会有一些问题就是我们写过的各种各样的代码#xff0c;局部变量#xff0c;全局变量#xff0c;静态变量等它们到底是存在哪的呢#xff1f;相信有很多人在学C/C的时候会有这些疑问#xff…一C/C的内存分布1.1C/C内存分布在C语言阶段学习的时候总会有一些问题就是我们写过的各种各样的代码局部变量全局变量静态变量等它们到底是存在哪的呢相信有很多人在学C/C的时候会有这些疑问下面就来看看C/C中的内存分布代码语言javascriptAI代码解释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/7592326493200482841www.dongchedi.com/article/7592329049750995481www.dongchedi.com/article/7592327070701961753www.dongchedi.com/article/7592322906135413273www.dongchedi.com/article/7592308231180026392www.dongchedi.com/article/7592331190716498457www.dongchedi.com/article/7592320610865676825www.dongchedi.com/article/7592326493201007129www.dongchedi.com/article/7592322714795688510www.dongchedi.com/article/7592327392942178841www.dongchedi.com/article/7592319617927283225www.dongchedi.com/article/7592327476282638873www.dongchedi.com/article/7592319094112846360www.dongchedi.com/article/7592326854393152025www.dongchedi.com/article/7592319386741244441www.dongchedi.com/article/7592321316729504281www.dongchedi.com/article/7592319724022039102www.dongchedi.com/article/7592327140348379672www.dongchedi.com/article/7592319412238418457www.dongchedi.com/article/7592319724022137406www.dongchedi.com/article/7592319070578917950www.dongchedi.com/article/7592317677180486168www.dongchedi.com/article/7592331430680953368www.dongchedi.com/article/7592317943112057368www.dongchedi.com/article/7592312482736685593www.dongchedi.com/article/7592326787452011070www.dongchedi.com/article/7592320171805934142www.dongchedi.com/article/7592312175293891097www.dongchedi.com/article/7592326095602221593www.dongchedi.com/article/7592313460353466942www.dongchedi.com/article/7592315921234985497www.dongchedi.com/article/7592318584517493310www.dongchedi.com/article/7592313903104164377www.dongchedi.com/article/7592314981853905432www.dongchedi.com/article/7592327419139654169www.dongchedi.com/article/7592312482736620057www.dongchedi.com/article/7592327392941589017www.dongchedi.com/article/7592313382368297497www.dongchedi.com/article/7592317900531548734www.dongchedi.com/article/7592315372943327769www.dongchedi.com/article/7592311873639432729www.dongchedi.com/article/7592316979160121918www.dongchedi.com/article/7592312719127511577www.dongchedi.com/article/7592316786557616664www.dongchedi.com/article/7592327116290032153www.dongchedi.com/article/7592326416835002904www.dongchedi.com/article/7592313347400270360