此代码导致x
指向大小为100GB的内存块。为什么在重新分配小块时分配大块内存失败
#include <stdlib.h>
#include <stdio.h>
int main() {
auto x = malloc(1);
for (int i = 1; i< 1024; ++i) x = realloc(x, i*1024ULL*1024*100);
while (true); // Give us time to check top
}
虽然此代码分配失败。
#include <stdlib.h>
#include <stdio.h>
int main() {
auto x = malloc(1024ULL*1024*100*1024);
printf("%llu\n", x);
while (true); // Give us time to check top
}
第二个代码如何失败?这只是分配没有显示在顶部?无论如何,我强烈建议检查'malloc()'和'realloc()'的返回值,以查看您的'malloc()'实现是否由于某种原因被释放。 – cmaster 2015-04-05 23:24:03
@cmaster我会在一分钟内编辑该问题。但是我在第二个例子中添加了一个检查并且'x'为空。但是,是的,它并没有出现在'top' – 2015-04-05 23:32:46