上代码直接研究:
int main() { int *heap_d; int *heap_e; int *heap_f; heap_d = (int *)malloc(10); heap_e = (int *)malloc(10); printf("The d address is %p\n",heap_d); printf("The e address is %p\n",heap_e); free(heap_d); heap_d = NULL; heap_f = (int *)malloc(30); printf("The f address is %p\n",heap_f); return 0; }
输出:
The d address is 0xf0d010 mem_d The e address is 0xf0d030 mem_e The f address is 0xf0d460 mem_f 可想而知,总共三段内存分配
mem_d|mem_e|
free
|mem_e|
|mem_f|
|xxxx| | |
xxx为无用内存,碎片,即使分配后已经free和置NULL操作。
越来越多的malloc使用,会促进内存碎片化加剧,最终内存不足。
Life is mess, don't let mess mess us.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了