C++ 尽管内存为32 GB,但无法分配3 GB浮点指针

C++ 尽管内存为32 GB,但无法分配3 GB浮点指针,c++,linux,memory,C++,Linux,Memory,我需要为808704000个浮点数分配内存,大约3085MB。我的电脑有32 GB内存,运行64位Linux(CentOS 6.6)。每次我尝试分配内存时,malloc操作都会失败。我使用g++4.4.7 有人能解释为什么我不能分配内存吗?是否可能以某种方式强制程序以64位模式编译 void AllocateMemory(float *& pointer, int size, void** pointers, int& Npointers,

我需要为808704000个浮点数分配内存,大约3085MB。我的电脑有32 GB内存,运行64位Linux(CentOS 6.6)。每次我尝试分配内存时,malloc操作都会失败。我使用g++4.4.7

有人能解释为什么我不能分配内存吗?是否可能以某种方式强制程序以64位模式编译

void AllocateMemory(float *& pointer, int size, void** pointers,
                    int& Npointers, nifti_image** niftiImages,
                    int Nimages, const char* variable)
{
    pointer = (float*)malloc(size);
    if (pointer != NULL)
    {
        pointers[Npointers] = (void*)pointer;
        Npointers++;
    }
    else
    {
        printf("Could not allocate host memory for variable %s !\n",
               variable);        
        FreeAllMemory(pointers, Npointers);
        FreeAllNiftiImages(niftiImages, Nimages);
        exit(EXIT_FAILURE);        
    }
}
ulimit-a
打印:

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 256261
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 1024
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

将其设置为
size\u t size
int
通常为32位,包括符号,因此最大值为2^31=2147483648 因此,
int(sizeof(float)*808704000)<0
是一个溢出


然后,由于
malloc
需要一个
size\u t
,它将对其进行符号扩展到64位,然后重新解释为无符号,给出一个大于2^63的大数字。(谢谢。)

制作
size\u t size
int
通常为32位,包括符号,因此最大值为2^31=2147483648 因此,
int(sizeof(float)*808704000)<0
是一个溢出


然后,由于
malloc
需要一个
size\u t
,它将对其进行符号扩展到64位,然后重新解释为无符号,给出一个大于2^63的大数字。(谢谢。)

在程序中使用
strace
。失败的
malloc
正在设置
errno
,因此在这种情况下使用
perror
。顺便说一下,C++中最好使用<代码>新< /C>。并在此显示一些源代码。您确定您的可执行文件正在编译为64位吗?(64位系统可以运行32位可执行文件)。如果在可执行文件上运行
file
,会发生什么情况?另外,签出可以在shell中显示
ulimit-a
的输出吗?它显示程序的限制,检查数据和堆栈值。在程序上使用
strace
。失败的
malloc
正在设置
errno
,因此在这种情况下使用
perror
。顺便说一下,C++中最好使用<代码>新< /C>。并在此显示一些源代码。您确定您的可执行文件正在编译为64位吗?(64位系统可以运行32位可执行文件)。如果在可执行文件上运行
file
,会发生什么情况?另外,签出可以在shell中显示
ulimit-a
的输出吗?它显示程序的限制,检查数据和堆栈值。若要添加到此答案,
(int)3234816000
为负数,符号扩展为64位,然后重新解释为无符号,给出了巨大的数字>2^63。若要添加到此答案,
(int)3234816000
为负数,符号扩展为64位,然后重新解释为无符号,给出一个大于2^63的巨大数字。