ani*_*udh 0 c memory arrays memory-management
我在通话时面临一个奇怪的问题malloc。我正在开发一个使用巨大数组(大小以 GB 为单位)的程序,并在尝试使用malloc该数组为数组分配内存时发现,malloc即使我分配的大小大于我的 RAM(64GB),它也是成功的。
参见下面的代码:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define Sixteen_G 16000000000
int main() {
int *c = (int *)malloc(sizeof(int)*Sixteen_G);
int *d = (int *)malloc(sizeof(int)*Sixteen_G);
int *e = (int *)malloc(sizeof(int)*Sixteen_G);
int *f = (int *)malloc(sizeof(int)*Sixteen_G);
int *g = (int *)malloc(sizeof(int)*Sixteen_G);
if(c == NULL)
printf("c Allocation failed\n");
if(d == NULL)
printf("d Allocation failed\n");
if(e == NULL)
printf("e Allocation failed\n");
if(f == NULL)
printf("e Allocation failed\n");
if(g == NULL)
printf("e Allocation failed\n");
else
printf("All arrays allocated\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上述代码的输出是:
All arrays allocated
Run Code Online (Sandbox Code Playgroud)
malloc此外,对size 的单个调用>= 17GB失败,但对 size 的多个调用正在16GB传递。
有人可以解释为什么 malloc 能够分配那么多内存,当我的系统 RAM 大小只有 64GB 时,以及malloc在单个/多个调用上到底如何工作