在64位上4GB之后的bad_alloc [英] bad_alloc after 4GB on 64bit

查看:101
本文介绍了在64位上4GB之后的bad_alloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,为什么这会失败引发bad_alloc

int * v = new int [6000000000];


如果成功

int * v =(int *)malloc((unsigned)6000000000)


在同一台机器上,相同的编译器g ++,64bit linux red hat

enterprise 4,没有用户的ulimits,足够的虚拟内存ecc ...


在这两种情况下使用

g ++ source.cpp -o executable编译

文件总是一个c ++ .cpp文件,上面那行是唯一一个

不同的文件。


谢谢

Hi guys, why does this fail raising bad_alloc
int *v = new int [6000000000];

if this succeeds
int *v = (int *) malloc((unsigned)6000000000)

both on the same machine, same compiler g++, 64bit linux red hat
enterprise 4, no ulimits on the user, enough virtual memory ecc...

in both cases compiled with
g++ source.cpp -o executable
the file is always a c++ .cpp file, that line above is the only one that
differs.

Thanks

推荐答案

cman写道:
cman wrote:

大家好,为什么这会失败提高bad_alloc

int * v = new int [6000000000];
Hi guys, why does this fail raising bad_alloc
int *v = new int [6000000000];



AFAIK,任何整数文字都将被解释为32位有符号整数,即使编译器是64位编译器也是如此。我认为这对于32位编译器来说是向后兼容的原因。如果你想要一个64位的int literal的int literal,你必须在后面添加LL:6000000000LL应该

这样做。

AFAIK, any integer literal will be interpreted as 32 bit signed integer, even
though the compiler is a 64bit compiler. I think that this is meant to be so for
backward compatibility reasons to 32bit compilers. If you want an int literal to
be a 64bit int literal, you''ll have to add LL at the back: 6000000000LL should
do the trick.


如果成功

int * v =(int *)malloc((unsigned)6000000000)


都在同一台机器上,同样的编译器g ++,64bit linux red hat

enterprise 4,没有用户的ulimits,足够的虚拟内存ecc ...


在这两种情况下使用

g ++ source.cpp -o可执行文件来源

该文件始终是一个c ++ .cpp文件,上面的那一行是唯一一个

不同。
if this succeeds
int *v = (int *) malloc((unsigned)6000000000)

both on the same machine, same compiler g++, 64bit linux red hat
enterprise 4, no ulimits on the user, enough virtual memory ecc...

in both cases compiled with
g++ source.cpp -o executable
the file is always a c++ .cpp file, that line above is the only one that
differs.



问候,

Stuart

Regards,
Stuart


int * v = new int [6000000000 ];


请求24000000000字节。
int *v = new int [6000000000];

Request for 24000000000 bytes.

int * v =(int *)malloc((unsigned)6000000000)
int *v = (int *) malloc((unsigned)6000000000)



请求6000000000字节。

Leo Havm?ller。

Request for 6000000000 bytes.

Leo Havm?ller.


int * v = new int [6000000000];
int *v = new int [6000000000];

>

请求24000000000字节。
>
Request for 24000000000 bytes.



所以int是4个字节?

So an int is 4 bytes?


这篇关于在64位上4GB之后的bad_alloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆