此代碼導致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