0
我有存儲器碎片的問題可以以這種小例子來概括:這是內存碎片嗎? (視覺工作室和MinGW)
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
void *p[8000];int i,j;
p[0]=malloc(0x7F000);
if (p[0]==NULL)
printf("Alloc after failed!\n");
else
free(p[0]);
for (i=0;i<8000; i++) {
p[i]=malloc(0x40000);
if (p[i]==NULL){
printf("alloc failed for i=%d\n",i);
break;
}
}
for(j=0;j<i;j++) {
free(p[j]);
}
/*Alloc 1 will fail, Alloc 2 *might* fail, AlloC3 succeeds*/
p[0]=malloc(0x7F000);
if (p[0]==NULL)
printf("Alloc1 after failed!\n");
else {printf("alloc1 success\n");free(p[0]);}
p[0]=malloc(0x40000);
if (p[0]==NULL)
printf("Alloc2 after failed!\n");
else {printf("alloc2 success\n");free(p[0]);}
p[0]=malloc(0x10000);
if (p[0]==NULL)
printf("Alloc3 after failed!\n");
else {printf("alloc3 success\n");free(p[0]);}
printf("end");
}
該程序打印(Win7上編譯MSVC(都與調試和releas分配器)和MinGW ):
alloc failed for i=7896
Alloc1 after failed!
alloc2 success
alloc3 success
end
是否有反正我可以避免這種情況?在我真正的應用程序中,我無法避免這種情況,我的程序達到了2GB的內存限制......但我希望能夠通過釋放某些東西來繼續。
爲什麼碎片在這個小例子中首先發生?當我開始執行「free-s」時,爲什麼內存塊沒有壓縮,因爲它們應該是相鄰的。
謝謝!