Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 64位Ubuntu box上的Malloc失败_Linux_Ubuntu_Memory Management_Malloc_Libsvm - Fatal编程技术网

Linux 64位Ubuntu box上的Malloc失败

Linux 64位Ubuntu box上的Malloc失败,linux,ubuntu,memory-management,malloc,libsvm,Linux,Ubuntu,Memory Management,Malloc,Libsvm,我在一个有18GB内存的64位Ubuntu设备上运行下面的代码,正如你所看到的,当我试图分配2^31字节时,我对Malloc的调用失败了。我不知道为什么会发生这种情况,或者如何修复它(我尝试了编译器标志和calloc()。我想知道是否有人能向我解释为什么我不能在64位的盒子上分配更多的空间,以及如何解决这个问题 #include <stdio.h> #include <stdlib.h> //#include "svm_model_matlab.h" //include

我在一个有18GB内存的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;
}
更新:

我发现了我遇到的问题:我目前在64位Ubuntu linux发行版的EC2上运行代码,EC2上的默认linux框有0交换空间。这导致我的进程在请求超过物理RAM的内存量时出现seg故障,因为它无法分页。创建交换文件后,我的问题就解决了


谢谢你的帮助

pow()
是计算2的幂的可怕方法。使用
1
pow()
计算2的幂是一种可怕的方法。使用
1 printf(“%d”,sizeof(svm_节点))并告诉我们结果printf(“%d”,sizeof(svm_节点)),然后告诉我们结果感谢您的帮助。我明天会检查这个,但我确实尝试将svm_节点中的int改为long、long-long等,我仍然会看到溢出。此外,这只是一个简单的程序,用于解决libsvm中的一个更大问题,其中我的训练集大小太大,以至于我试图将malloc~2^33.1字节用于模拟,malloc失败,我正在进行seg故障处理。这就是我试图解决的问题,这个简单的例子的目的是试图用一种更容易理解的格式重新生成这个问题我不确定我是否遵循了你的逻辑ben:你说的是:“接下来,你分配的字节远远超过了你问题中提到的231个字节,你实际上是在分配2i*sizeof(svm_节点),或大约2i+4。i=30时,分配失败的内存大约为16GB,这很可能比您的可用内存还要多。”。我不明白你是怎么计算的。如上所示,通过我的计算,sizeof(svm_节点)=16字节2^30 1073741824*sizeof(svm_节点)=17179869 KBsmy bad ben,2^30 1073741824*sizeof(svm_节点)=17 Gbs感谢您的帮助。我明天会检查这个,但我确实尝试将svm_节点中的int改为long、long-long等,我仍然会看到溢出。此外,这只是一个简单的程序,用于解决libsvm中的一个更大问题,其中我的训练集大小太大,以至于我试图将malloc~2^33.1字节用于模拟,malloc失败,我正在进行seg故障处理。这就是我试图解决的问题,这个简单的例子的目的是试图用一种更容易理解的格式重新生成这个问题我不确定我是否遵循了你的逻辑ben:你说的是:“接下来,你分配的字节远远超过了你问题中提到的231个字节,你实际上是在分配2i*sizeof(svm_节点),或大约2i+4。i=30时,分配失败的内存大约为16GB,这很可能比您的可用内存还要多。”。我不明白你是怎么计算的。如上所示,通过我的计算,sizeof(svm_节点)=16字节2^30 1073741824*sizeof(svm_节点)=17179869 KBsmy bad ben,2^30 1073741824*sizeof(svm_节点)=17 GBs
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