c - 如何在 c 中分配大容量(GB 数量级)的内存?

标签 c

我正在尝试使用 c 语言中的 malloc 来为我们分配大量数据。当我将数组大小增加到超过特定值时,程序终止说“program.c”停止工作。我想知道正确的方法将大量数据(这里是 4GB)分配给 c 中的一般变量。我在 32GB RAM 上使用 64 位窗口。这是供引用的代码。

   #include<stdio.h>
   #include<math.h>
   #include<conio.h>
   #include<stdlib.h>
   double** Allocate(int d1,int d2);
   double** Allocate(int d1,int d2)
   {  
      double **m;
      int i,j;
      m = (double**)malloc(sizeof(double*)*d1);  
      for(i=0;i<d1;i++)
      {
       m[i]=(double*)malloc(sizeof(double)*d2);
      }
     for(i=0;i<d1;i++)
       {
        for(j=0;j<d2;j++)
         {
           m[i][j]=0.0;
         }
       }
     return m;
   }
   main()
   {
    int i,j;
    int n=2.2e4;
    double** A= Allocate(n,n);  
    printf("Memory allocation successful");
   }

最佳答案

您正在分配 3e5*3e5 = 9e10 double ,这表示 720 Go 假设 double 为 8 个八位字节。

关于c - 如何在 c 中分配大容量(GB 数量级)的内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39434011/

相关文章:

在 C 语言的 fprintf 语法中调用函数

c++ - 为游戏优化 LAN 服务器

c - 关于C中的switch case,为什么打印不出来?

c++ - 从 C 调用静态 C++ 方法在编译时给出 undefined reference

c - 打印的值前面有不同数量的空格吗?

c - ansi C 对内存中程序的大小有限制吗?

C - sizeof int 数组总是返回 4

python - 用C从Chrome中解密 "Saved Password"

c - 为什么在我的代码中使用宏会导致错误以及如何修复它?

c - 递增单个字符以输出到 OLED 显示器,但递增 2 而不是 1