背景
malloc 的基本原理,之前的几篇文章已经介绍过了。这里主要想说明一下根据mallinfo的信息来分析内存实际的问题。
malloc后立即free
void test2()
{
print_info();
cout << hex << sbrk(0) << endl;
for ( int i = 0; i < 1; ++i )
{
free(malloc(1024));
}
cout << "------------------------------------------" << endl;
cout << hex << sbrk(0) << endl;
print_info();
}
结果如下图
我们可以知道edata指针被推高了0x21000即135168个字节。
无论是malloc几次,只要未超过edata,都不会再将edata推高。