#include <stdio.h> #include <stdlib.h> int main() { int MB = 0; while(malloc(1 << 20)) ++MB; printf("Allocated %d MB total\n",MB); }
爲了讓程序可以在內存限制的機器上運行,能夠把每次分配的1MB改爲1KBcode
1 << 20,改爲1<<10,用kb代替mb內存