4

【C++内存管理】11_G2.9 std::alloc 概念大整理

 3 years ago
source link: https://segmentfault.com/a/1190000040048099
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

概念大整理

image.png

补充说明

  • list 节点向分配器请求的内存大小: sizeof(Foo) + sizeof(node<Foo>*) x 2

image.png

学习使用: "常量 == 变量" 格式, 当漏泄等号时编译器会发出编译错误提示

注:较新的编译器在 "常量 = 变量" 时通常也会发出编译警告


image.png

不推荐使用:对象的定义与使用不应间隔太远,而应做到尽量接近(间隔中途可能被修改,尤其是指针时)


image.png

image.png

不推荐使用:可读性不高,应该尽量分行书写


image.png

不推荐使用:可读性不高

形式相同 :

image.png


image.png

在同一个多进程的系统中,竭泽而渔会对其它进程产生灾难性影响

对应到前一章最后的疑惑问题 2 :

system heap 手中还剩多少资源? 10000 - 9688 = 312.
可不可以将失败的那次索取折半...再折半...再折半...最终当索取量 <= 312 便能得到满足

image.png

缺陷:内存未归还系统

使用 malloc 在操作系统中进行内存申请,申请的内存会携带 cookie 进行标记,以辅助后续 free 进行内存释放
在下图可见, 标注的起始内存块已不被自由链表管理,同时也无法确定对应 cookie 的内存块都连续空闲

image.png


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK