简体   繁体   English

Malloc在64位Ubuntu机器上失败

[英]Malloc Failing on 64-Bit Ubuntu box

I am running the following code on a 64-bit Ubuntu box with 18 GB of RAM, and as you can see, my call to Malloc is failing when I try to allocate 2^31 bytes. 我在具有18 GB RAM的64位Ubuntu机器上运行以下代码,并且如您所见,当我尝试分配2 ^ 31字节时,对Malloc的调用失败。 I am not sure why this is happening, or how to fix it (i have tried compiler flags and also calloc()). 我不确定为什么会这样,或者如何解决(我已经尝试过编译器标志以及calloc())。 I was wondering if someone can explain to me why I am not able to alloc more space on a 64-bit box and how I can fix this issue. 我想知道是否有人可以向我解释为什么我不能在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;
}

Output: 输出:

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

Update: 更新:

I found the issue I was having: I am currently running my code on EC2 on a 64-bit Ubuntu linux distro and the default linux boxes on EC2 have 0 swap space. 我发现了我遇到的问题:我目前在64位Ubuntu linux发行版上的EC2上运行我的代码,EC2上的默认linux框的交换空间为0。 This was causing my process to seg fault when it was requesting any amount of memory more than the physical RAM because it was not able to page. 当它请求的内存量大于物理RAM时,这导致我的进程出现故障,因为它无法分页。 After I created a swap file, my problem went away. 创建交换文件后,我的问题消失了。

Thanks for your help 谢谢你的帮助

pow() is a horrible way to calculate powers of 2. Use 1 << i instead. pow()是一种计算2的幂的可怕方法。请改用1 << i

Then, pick a data type big enough to hold your requested size. 然后,选择一个足以容纳您要求的大小的数据类型。 Right now it's overflowing the size of int , and therefore trying to allocate a negative number of bytes. 现在,它溢出了int的大小,因此尝试分配负数的字节。 That doesn't work for obvious reasons. 由于明显的原因,这不起作用。

I suspect that malloc(1ULL << 31) would succeed on your system with no issues whatsoever. 我怀疑malloc(1ULL << 31)可以在您的系统上成功运行,没有任何问题。

Next, you're allocating far more than the 2 31 bytes your question mentions, you're actually trying to allocate 2 i * sizeof (svm_node) , or about 2 i+4 . 接下来,您要分配的空间远远超过问题提到的2 31字节,实际上是在分配2 i * sizeof (svm_node)或大约2 i + 4 The failing allocation, with i=30 , is for roughly 16GB, which may very well be more than your free RAM. i=30的失败分配大约是16GB,这可能比您的可用RAM还要大。

Finally, you're getting 32-bit values when printing pointers. 最后,在打印指针时将获得32位值。 Try printf("%p", x_space); 尝试printf("%p", x_space); instead. 代替。 If that still gives you 32-bit values, try using a 64-bit compiler. 如果仍然可以提供32位值,请尝试使用64位编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Ubuntu 64 上运行 ELF 64 位 LSB 可执行文件 - Run ELF 64-bit LSB executable on Ubuntu 64 目标“全部”的配方在Ubuntu 15.10 64位上失败 - recipe for target 'all' failed on Ubuntu 15.10 64-bit 无法在 Ubuntu 16.04(64 位)上安装 Snap7 库 - Unable to install Snap7 Library on Ubuntu 16.04 (64-bit) 如何在64位Ubuntu 18.04上使用64位clang v8构建32位libc ++? - How do I build 32-bit libc++ with 64-bit clang v8 on 64-bit Ubuntu 18.04? 64位Ubuntu 16.04上的32位编译失败 - 32 bit compilation failing on 64 bit Ubuntu 16.04 在Ubuntu 13中的64位环境中构建32位FCGI ++二进制文件 - Building 32-bit FCGI++ binaries in 64-bit environment in Ubuntu 13 在64位Ubuntu12.04下编译LinSched 3.3时math.h引用失败 - math.h refer failure when compile LinSched 3.3 under 64-bit Ubuntu12.04 PC 在 Ubuntu 10.04(64 位机器)上访问物理 memory 时完全冻结 - PC freezes totally while accessing physical memory on Ubuntu 10.04(64-bit machine) Pyinstaller无法在Python2.7 64位Ubuntu中编译大型程序 - Pyinstaller Not Compiling Large Program in Python2.7 64-Bit Ubuntu 如何从Ubuntu 12.04(64位)中删除所有Android -Tools(ADB和fastboot)以及SDK - How to Remove all Android -Tools (ADB and fastboot) plus SDK from Ubuntu 12.04 (64-bit)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM