在c++中数组有最大长度吗?
这是c++的限制还是取决于我的机器?它是否可以调整?它取决于数组的类型吗?
我能以某种方式打破这个限制吗?还是我必须寻找更好的存储信息的方式?最简单的方法是什么?
我要做的是在一个数组中存储long long int,我在Linux环境中工作。我的问题是:如果我需要存储一个包含N个长整数和N个>个10位数字的数组,我该怎么做?
我需要这个,因为我正在为学校写一些加密算法(例如p-Pollard),并遇到了整数和数组长度表示的这堵墙。
在c++中数组有最大长度吗?
这是c++的限制还是取决于我的机器?它是否可以调整?它取决于数组的类型吗?
我能以某种方式打破这个限制吗?还是我必须寻找更好的存储信息的方式?最简单的方法是什么?
我要做的是在一个数组中存储long long int,我在Linux环境中工作。我的问题是:如果我需要存储一个包含N个长整数和N个>个10位数字的数组,我该怎么做?
我需要这个,因为我正在为学校写一些加密算法(例如p-Pollard),并遇到了整数和数组长度表示的这堵墙。
当前回答
我会通过创建一个2d动态数组来解决这个问题:
long long** a = new long long*[x];
for (unsigned i = 0; i < x; i++) a[i] = new long long[y];
更多信息请访问https://stackoverflow.com/a/936702/3517001
其他回答
有一件事我认为在之前的回答中没有提到。
当人们在设计中使用这些东西时,我总是感觉到重构的“臭味”。
这是一个巨大的数组,从效率和性能的角度来看,这可能不是表示数据的最佳方式。
欢呼,
Rob
从实际而非理论的角度来看,在32位Windows系统上,单个进程可用的最大内存总量是2 GB。您可以通过使用具有更多物理内存的64位操作系统来打破这个限制,但是是这样做还是寻找替代方案在很大程度上取决于您的预期用户和他们的预算。您还可以使用PAE对其进行某种程度的扩展。
数组的类型非常重要,因为许多编译器上的默认结构对齐是8字节,如果内存使用有问题,这是非常浪费的。如果你使用Visual c++瞄准Windows,可以使用#pragma pack指令来克服这个问题。
另一件要做的事情是看看哪些内存压缩技术可以帮助你,比如稀疏矩阵,动态压缩等等……这也是高度依赖于应用程序的。如果你编辑你的文章来提供更多关于数组中实际内容的信息,你可能会得到更有用的答案。
Edit: Given a bit more information on your exact requirements, your storage needs appear to be between 7.6 GB and 76 GB uncompressed, which would require a rather expensive 64 bit box to store as an array in memory in C++. It raises the question why do you want to store the data in memory, where one presumes for speed of access, and to allow random access. The best way to store this data outside of an array is pretty much based on how you want to access it. If you need to access array members randomly, for most applications there tend to be ways of grouping clumps of data that tend to get accessed at the same time. For example, in large GIS and spatial databases, data often gets tiled by geographic area. In C++ programming terms you can override the [] array operator to fetch portions of your data from external storage as required.
前面已经指出,数组大小受硬件和操作系统的限制(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)).
没有人提到堆栈帧的大小限制。
有两个地方可以分配内存:
在堆上(动态分配内存)。 这里的大小限制是可用硬件和操作系统通过使用其他设备临时存储未使用的数据(即将页面移动到硬盘)来模拟空间的能力的组合。 在堆栈上(局部声明的变量)。 这里的大小限制是编译器定义的(可能有硬件限制)。如果你阅读编译器文档,你经常可以调整这个大小。
因此,如果你动态分配一个数组(限制很大,详见其他文章)。
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
我同意上面的观点,如果你用
int myArray[SIZE]
那么SIZE受限于一个整数的大小。但是你总是可以malloc一个内存块,并有一个指向它的指针,只要malloc不返回NULL。