C++ int数组的最大大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12488477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:19:29  来源:igfitidea点击:

Maximum size of int array?

c++c

提问by Aesthete

Possible Duplicate:
C programming, why does this large array declaration produce a segmentation fault?

可能的重复:
C 编程,为什么这个大数组声明会产生分段错误?

I have written a simple program.

我写了一个简单的程序。

#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    int genotype[150000000];
}

But I get a strange error:

但我收到一个奇怪的错误:

RUN FAILED (exit value 1, total time: 131ms)

RUN FAILED(退出值1,总时间:131ms)

How can I save this amount of int?

我怎样才能节省这笔钱int

(I have enough memory to save this amount of ints and my computer is 64-bit)

(我有足够的内存来保存这个数量的ints,我的电脑是 64 位的)

回答by NullPoiиteя

Your stack is too small. Put this on the heap, using new:

你的筹码太少了。把它放在堆上,使用新的:

int* genotype = new int[150000000];

And i hope that following will be useful.

我希望以下内容会有所帮助。

  • signed char: -127 to 127 (note, not -128 to 127; this accommodates 1's-complement platforms)
  • unsigned char: 0 to 255
  • "plain" char: -127 to 127 or 0 to 255 (depends on default char signedness)
  • signed short: -32767 to 32767
  • unsigned short: 0 to 65535
  • signed int: -32767 to 32767
  • unsigned int: 0 to 65535
  • signed long: -2147483647 to 2147483647
  • unsigned long: 0 to 4294967295
  • signed long long: -9223372036854775807 to 9223372036854775807
  • unsigned long long: 0 to 18446744073709551615
  • 带符号的字符:-127 到 127(注意,不是 -128 到 127;这适用于 1 的补码平台)
  • 无符号字符:0 到 255
  • “普通”字符:-127 到 127 或 0 到 255(取决于默认字符符号)
  • 签名空头:-32767 到 32767
  • 无符号短:0 到 65535
  • 签名整数:-32767 到 32767
  • 无符号整数:0 到 65535
  • 签名长:-2147483647 到 2147483647
  • 无符号长:0 到 4294967295
  • 长长签名:-9223372036854775807 到 9223372036854775807
  • 无符号长长:0 到 18446744073709551615

Or you can use limit.h to find out around what your program relies on. For example, this is how you will find maximum range for int:

或者您可以使用 limit.h 来了解您的程序所依赖的内容。例如,您可以通过以下方式找到 int 的最大范围:

C:

C:

#include <limits.h>
const int min_int = INT_MIN;
const int max_int = INT_MAX;

C++

C++

#include <limits>
const int min_int = std::numeric_limits<int>::min();
const int max_int = std::numeric_limits<int>::max();

回答by Aesthete

As a linker option...

作为链接器选项...

/STACK 601048576 //  150000000 * 4 (assume size of int), and 1Mb for general.

But hey.. Don't do this. I'm not even sure what will happen.

但是嘿..不要这样做。我什至不确定会发生什么。

回答by Vaughn Cato

You are allocating this on the stack, which is more limited than the heap or static storage.

您在堆栈上分配它,这比堆或静态存储更受限制。

For C++, a std::vectorwould be a better option.

对于 C++, astd::vector将是更好的选择。

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
    vector<int> genotype(150000000);
}

回答by Avrillavigne

If you want to use dynamic memory simply declare a pointer to the type you want to allocate in this case int, and call the malloc() function with the argument being the number of bytes you want the "array" to be. In this case you need to get the sizeof a single integer and multiply it by how big you want it to be (i.e. the number of cells):

如果您想使用动态内存,只需声明一个指向您要在这种情况下分配的类型的指针 int,然后调用 malloc() 函数,参数是您希望“数组”的字节数。在这种情况下,您需要获取单个整数的大小并将其乘以您想要的大小(即单元格数):

Make sure you free anything you malloc'ed otherwise you will get memory leaks.

确保你释放了任何你 malloc 的东西,否则你会得到内存泄漏。