程序:
运行结果:
疑问:
虚拟机的物理内存是1G, 但是可以申请1.7G的堆内存,这真是个奇妙?后续研究一下
https://www.nowcoder.com/discuss/20610?type=0&order=0&pos=6&page=1
代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int cnt = 0;
void* tmp = NULL;
while (true)
{
cnt += 100;
tmp = malloc(cnt * 1024 * 1024);
if (tmp)
{
printf("malloc %d M ok.\n", cnt);
free(tmp);
}
else
{
printf("malloc %d M failed.\n", cnt);
break;
}
}
printf("the test is over.\n");
}