gpt4 book ai didi

c - C 中的 Malloc 内存消耗行为

转载 作者:太空狗 更新时间:2023-10-29 16:12:24 25 4
gpt4 key购买 nike

<分区>

我编写这个程序是为了了解内存消耗模式。

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i=0;

while(1 < 2) {
int *str = (int *) malloc(100000000);

if(str == NULL) {
printf("Out of memory.. %d", i);
return(1);
}
printf("Attempt Count = %d\n", i++);

}

return(0);
}

在我的系统中,我有 8 GB 内存。每个 malloc 调用将尝试存储 100MB。所以我期望我的程序会在 (10* 8) 循环中崩溃。但是计数器的值远远超过 600,000。我无法得出同样的结论。请帮忙。

即使考虑到所有交换空间,它也不应该增加超过 16GB,这在这里看起来不太可能。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com