大数组大小分段故障 [英] Segmentation fault on large array sizes

查看:126
本文介绍了大数组大小分段故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code给我分割故障时,2GB的机器上运行,但工程4GB的机器上。

The following code gives me a segmentation fault when run on a 2Gb machine, but works on a 4GB machine.

int main()
{
   int c[1000000];
   cout << "done\n";
   return 0;
}

数组的大小恰好4Mb的。是否有任何可以在C中使用数组的大小限制++?

The size of the array is just 4Mb. Is there a limit on the size of an array that can be used in c++?

推荐答案

你可能刚开堆栈溢出这里。该数组是太大,不适合在你的程序的堆栈地址空间。

You're probably just getting a stack overflow here. The array is too big to fit in your program's stack address space.

如果您分配你应该罚款堆阵列,假设你的机器有足够的内存。

If you allocate the array on the heap you should be fine, assuming your machine has enough memory.

为int *阵列=新的INT [百万];

但是,请记住,这将要求您删除[] 的数组。一个更好的解决办法是使用的std ::矢量&lt; INT方式&gt; ,并调整其大小为1000000元

But remember that this will require you to delete[] the array. A better solution would be to use std::vector<int> and resize it to 1000000 elements.

这篇关于大数组大小分段故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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