在c++中数组有最大长度吗?

这是c++的限制还是取决于我的机器?它是否可以调整?它取决于数组的类型吗?

我能以某种方式打破这个限制吗?还是我必须寻找更好的存储信息的方式?最简单的方法是什么?

我要做的是在一个数组中存储long long int,我在Linux环境中工作。我的问题是:如果我需要存储一个包含N个长整数和N个>个10位数字的数组,我该怎么做?

我需要这个,因为我正在为学校写一些加密算法(例如p-Pollard),并遇到了整数和数组长度表示的这堵墙。


当前回答

前面已经指出,数组大小受硬件和操作系统的限制(man ulimit)。不过,你的软件可能只受限于你的创造力。例如,您可以将“数组”存储在磁盘上吗?你真的需要长整型吗?你真的需要密集数组吗?你需要数组吗?

One simple solution would be to use 64 bit Linux. Even if you do not physically have enough ram for your array, the OS will allow you to allocate memory as if you do since the virtual memory available to your process is likely much larger than the physical memory. If you really need to access everything in the array, this amounts to storing it on disk. Depending on your access patterns, there may be more efficient ways of doing this (ie: using mmap(), or simply storing the data sequentially in a file (in which case 32 bit Linux would suffice)).

其他回答

如果你必须处理这么大的数据,你就需要把它分成易于管理的块。在任何小型计算机的内存中都装不下这些数据。你可以 从磁盘加载一部分数据(任何合理合适的数据),执行计算并对其进行更改,将其存储到磁盘,然后重复操作,直到完成。

前面已经指出,数组大小受硬件和操作系统的限制(man ulimit)。不过,你的软件可能只受限于你的创造力。例如,您可以将“数组”存储在磁盘上吗?你真的需要长整型吗?你真的需要密集数组吗?你需要数组吗?

One simple solution would be to use 64 bit Linux. Even if you do not physically have enough ram for your array, the OS will allow you to allocate memory as if you do since the virtual memory available to your process is likely much larger than the physical memory. If you really need to access everything in the array, this amounts to storing it on disk. Depending on your access patterns, there may be more efficient ways of doing this (ie: using mmap(), or simply storing the data sequentially in a file (in which case 32 bit Linux would suffice)).

正如许多优秀的答案所指出的,有很多限制取决于你的c++编译器版本、操作系统和计算机特性。但是,我建议使用以下Python脚本检查机器上的限制。

它使用二进制搜索,并在每次迭代中通过创建一个尝试创建该大小的数组的代码来检查中间大小是否可行。脚本尝试编译它(对不起,这部分只在Linux上工作),并根据成功与否调整二进制搜索。看看吧:

import os

cpp_source = 'int a[{}]; int main() {{ return 0; }}'

def check_if_array_size_compiles(size):
        #  Write to file 1.cpp
        f = open(name='1.cpp', mode='w')
        f.write(cpp_source.format(m))
        f.close()
        #  Attempt to compile
        os.system('g++ 1.cpp 2> errors')
        #  Read the errors files
        errors = open('errors', 'r').read()
        #  Return if there is no errors
        return len(errors) == 0

#  Make a binary search. Try to create array with size m and
#  adjust the r and l border depending on wheather we succeeded
#  or not
l = 0
r = 10 ** 50
while r - l > 1:
        m = (r + l) // 2
        if check_if_array_size_compiles(m):
                l = m
        else:
                r = m

answer = l + check_if_array_size_compiles(r)
print '{} is the maximum avaliable length'.format(answer)

您可以将它保存到您的机器并启动它,它将打印您可以创建的最大尺寸。我的机器是2305843009213693951。

有一件事我认为在之前的回答中没有提到。

当人们在设计中使用这些东西时,我总是感觉到重构的“臭味”。

这是一个巨大的数组,从效率和性能的角度来看,这可能不是表示数据的最佳方式。

欢呼,

Rob

没有人提到堆栈帧的大小限制。

有两个地方可以分配内存:

在堆上(动态分配内存)。 这里的大小限制是可用硬件和操作系统通过使用其他设备临时存储未使用的数据(即将页面移动到硬盘)来模拟空间的能力的组合。 在堆栈上(局部声明的变量)。 这里的大小限制是编译器定义的(可能有硬件限制)。如果你阅读编译器文档,你经常可以调整这个大小。

因此,如果你动态分配一个数组(限制很大,详见其他文章)。

int* a1 = new int[SIZE];  // SIZE limited only by OS/Hardware

或者,如果数组分配在堆栈上,则受限于堆栈帧的大小。注意:vector和其他容器在堆栈中存在的很小,但通常大部分数据都在堆上。

int a2[SIZE]; // SIZE limited by COMPILER to the size of the stack frame