我在具有 18 GB RAM 的 64 位 Ubuntu 机器上运行以下代码,如您所见,当我尝试分配 2^31 字节时,我对 Malloc 的调用失败了。我不确定为什么会这样,或者如何修复它(我已经尝试过编译器标志和 calloc())。我想知道是否有人可以向我解释为什么我不能在 64 位机器上分配更多空间以及如何解决这个问题。
#include <stdio.h>
#include <stdlib.h>
//#include "svm_model_matlab.h"
//include "svm.h"
#include <math.h>
struct svm_node
{
int index;
double value;
};
//#define Malloc(type,n) (type *)calloc(n,sizeof(type))
#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
int main()
{
int i;
for(i =25; i< 35; ++i)
{
printf("2^%d %d \n", i,(long int) pow(2,i));
svm_node* x_space = Malloc(struct svm_node,pow(2,i));
printf("the address is %x\n", x_space);
free(x_space);
}
return 0;
}
输出:
2^25 33554432
the address is 8513e010
2^26 67108864
the address is 6513e010
2^27 134217728
the address is 2513e010
2^28 268435456
the address is a513e010
2^29 536870912
the address is a513e010
2^30 1073741824
the address is 0
2^31 -2147483648
the address is 0
2^32 0
the address is 0
2^33 0
the address is 0
2^34 0
the address is 0
更新:
我发现了我遇到的问题:我目前正在 64 位 Ubuntu Linux 发行版上的 EC2 上运行我的代码,EC2 上的默认 Linux 机器有 0 个交换空间。这导致我的进程在请求比物理 RAM 多的内存时出现段错误,因为它无法分页。创建交换文件后,我的问题就消失了。
谢谢你的帮助
最佳答案
pow()
是计算 2 的幂的可怕方法。使用 1 << i
相反。
然后,选择一个足够大的数据类型来容纳您请求的大小。现在它溢出了 int
的大小,因此尝试分配负数的字节。由于显而易见的原因,这行不通。
我怀疑malloc(1ULL << 31)
会在您的系统上成功,没有任何问题。
接下来,您分配的远远超过问题提到的 231 字节,您实际上是在尝试分配 2i * sizeof (svm_node)
,或大约 2i+4。分配失败,i=30
,大约 16GB,这很可能比您的可用 RAM 多。
最后,打印指针时您将获得 32 位值。尝试 printf("%p", x_space);
反而。如果仍然为您提供 32 位值,请尝试使用 64 位编译器。
关于linux - Malloc 在 64 位 Ubuntu 机器上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5012334/