stl源代码破析中关于第二级配置器所遇到的问题.(读过stl源代码的请进)

hucong 2002-09-14 03:06:38
测试sgi stl的库
从sgi网站上下载for vc或者bcc的stl库,在vc环境中设置include就可以了

#include "vector.h"

void main()
{
vector<char> *pvc = new vector<char>(96);
delete pvc;
}

根据上述的例子,进行代码跟踪,结果得知是在内存的第二级配置器的chunk_alloc中
malloc( 2*96*20)这么多的内存.但却找不到到底什么地方进行了free操作.这个苦代码当然不会有内存漏洞了.读过stl源代码或者正在读的大侠们可以告诉这个问题的结果吗??
也希望有更多的人讨论一下.
...全文
85 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hucong 2002-09-14
  • 打赏
  • 举报
回复
只分配不释放,不是越来内存膨胀得越来越大??这样设计还是真是有胆量
不过这个代码是怎么欺骗探测器得
我自己随便写一个
char *p = (char*)malloc( 10 * sizeof(char));
只要不free就会报内存漏洞,而stl得库代码不会报漏洞.太过笨,看了半天也没看个端倪出来,唉~!
sttony 2002-09-14
  • 打赏
  • 举报
回复
这就是内存pool 的特点,不free,要释放的时候只是在内存pool中将这快标记为不用,如果将来申请的可以在内存pool 中放下就不用malloc了
陈硕 2002-09-14
  • 打赏
  • 举报
回复
Alloc does not free all memory

Memory allocated for blocks of small objects is not returned to malloc. It can only be reused by subsequent alloc::allocate requests of (approximately) the same size. Thus programs that use alloc may appear to leak memory when monitored by some simple leak detectors. This is intentional. Such "leaks" do not accumulate over time. Such "leaks" are not reported by garbage-collector-like leak detectors.

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧