C++ 创建大量数字(10^9 大小)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11358387/
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
Creating a large array of numbers (10^9 size)
提问by user1506119
I want to create an array that is capable of storing 10^9 numbers(long int).If i try doing this my compiler crashes.What is the maximum size array allowed in C++.Also if i do this dynamically too i get the same problem.How can i accomplish the task i am looking to acheive? Thanks, any help would be appreciated.
我想创建一个能够存储 10^9 个数字(长整型)的数组。如果我尝试这样做,我的编译器会崩溃。C++ 中允许的最大数组大小是多少。此外,如果我也动态执行此操作,我会得到相同的结果问题。我怎样才能完成我想要完成的任务?谢谢,任何帮助将不胜感激。
回答by Jonas Sch?fer
The maximum array size is dependent on the data you store (and the integers available to index them).
最大数组大小取决于您存储的数据(以及可用于索引它们的整数)。
So on a 32bit system, you can only index 232 elements at most if you're lucky, which is a bit above 10?. On a 64bit system, you can index 2?? elements, which is a bit above 101?. This is essentially the maximum array size. Being able to index that does not imply that you can also actually get that much from the operating system, as the actual virtual address space might be much smaller. On Linux, a virtual adress space of approx. 64 Terabytes is available per process on 64bit, which are 2?2 bytes.
所以在 32 位系统上,如果幸运的话,您最多只能索引 232 个元素,这比 10 多一点?。在 64 位系统上,您可以索引 2?? 元素,略高于 101?。这实质上是最大数组大小。能够索引并不意味着您实际上也可以从操作系统中获得那么多,因为实际的虚拟地址空间可能要小得多。在 Linux 上,大约有一个虚拟地址空间。在 64 位上,每个进程有 64 TB 可用,即 2?2 字节。
However, if you actually try to allocate this, you need that much amount of bytes! So if you try to allocate an array of long int
which will probably be 64bits of size, you need 8 Gigabytes of memory.
但是,如果您确实尝试分配它,则需要那么多字节!因此,如果您尝试分配一个long int
大小可能为 64 位的数组,则需要 8 GB 的内存。
On a 32bit system, this is impossible. On a 64bit system, you need to have that amount of ram and swap space to work.
在 32 位系统上,这是不可能的。在 64 位系统上,您需要有足够的内存和交换空间才能工作。
If you're on a 32bit system or on a 64bit system without enough memory, you'll get a out of memory error, which is probably the reason for the behaviour you see.
如果您使用的是 32 位系统或 64 位系统而没有足够的内存,则会出现内存不足错误,这可能是您看到的行为的原因。
If you also try to create the array statically in a .data section of your executable, the executable may end up with being 8 GBytes large, where you could run into filesystem limits (fat32 anyone?). Also the compiler probably chokes on the amount of data (on 32bit, it'll probably crash).
如果您还尝试在可执行文件的 .data 部分中静态创建数组,则可执行文件最终可能会达到 8 GB 大,您可能会遇到文件系统限制(任何人都使用fat32?)。此外,编译器可能会因数据量而窒息(在 32 位上,它可能会崩溃)。
If you're allocating on stack (this is, as a statically sized local variable array), you'll also run into stack limits on certain operating systems.
如果您在堆栈上分配(这是作为静态大小的局部变量数组),您还会在某些操作系统上遇到堆栈限制。
回答by Jon
An array of 10^9 longs would typically take up at least 4GB of memory, which would already be prohibitive in all 32-bit systems.
一个 10^9 长的数组通常会占用至少 4GB 的内存,这在所有 32 位系统中已经是禁止的。
Even if that much memory is available in a 64-bit system you certainly cannot expect to allocate 4GB on the stack like this:
即使在 64 位系统中有这么多内存可用,您当然也不能指望像这样在堆栈上分配 4GB:
void foo() {
long arr[1000000000]; // stack size is a typically few MBs!
}
回答by Saeed Amiri
I don't think it's your compiler crash, it's your memory crash (out of memory or something like this), e.g in windows 32 bit, at most you could use 2^32 bit of memory space which is smaller than 10^9*64 so you will get memory exception. You could use it by paging and loading smaller parts from file to memory.
我不认为这是您的编译器崩溃,而是您的内存崩溃(内存不足或类似的东西),例如在 Windows 32 位中,您最多可以使用 2^32 位的内存空间,它小于 10^9* 64 所以你会得到内存异常。您可以通过分页并将较小的部分从文件加载到内存来使用它。
Edit:As Tobias Langner mentioned in comments, compiler also could raise this error but original problem is from memory.
编辑:正如 Tobias Langner 在评论中提到的,编译器也可能引发此错误,但最初的问题来自内存。
回答by tomasz
I want to create an array
我想创建一个数组
Do you really need an array? In other words, do you require your integers to be in one memory block, or you simply want to access them by index? If you don't care about the memory layout, but still want to have fast access to elements, you should use std::deque
. Instead of allocating one chunk of memory, it'll store your numbers in many small chunks, so as long as you have enough memory to store all your numbers together, you'll be fine.
你真的需要一个数组吗?换句话说,您是否要求整数位于一个内存块中,或者您只是想通过索引访问它们?如果您不关心内存布局,但仍想快速访问元素,则应使用std::deque
. 它不会分配一块内存,而是将您的数字存储在许多小块中,因此只要您有足够的内存将所有数字存储在一起,就可以了。