创建大量数字(10^9 大小) [英] Creating a large array of numbers (10^9 size)

查看:31
本文介绍了创建大量数字(10^9 大小)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个能够存储 10^9 个数字(long int)的数组.如果我尝试这样做,我的编译器会崩溃.C++ 中允许的最大数组大小是多少.此外,如果我也动态地这样做,我遇到同样的问题.我怎样才能完成我想要完成的任务?谢谢,任何帮助将不胜感激.

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.

推荐答案

最大数组大小取决于您存储的数据(以及可用于索引它们的整数).

The maximum array size is dependent on the data you store (and the integers available to index them).

因此,在 32 位系统上,如果幸运的话,您最多只能索引 2³² 个元素,这比 10⁹ 多一点.在 64 位系统上,您可以索引 2⁶⁴ 个元素,略高于 10¹⁹.这实质上是最大数组大小.能够索引并不意味着您实际上也可以从操作系统中获得那么多,因为实际的虚拟地址空间可能要小得多.在 Linux 上,大约有一个虚拟地址空间.在 64 位上,每个进程有 64 TB 可用,即 2⁴² 字节.

So on a 32bit system, you can only index 2³² 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 10¹⁹. 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⁴² bytes.

然而,如果你真的尝试分配它,你需要那么多的字节数!因此,如果您尝试分配一个 long int 数组,该数组的大小可能为 64 位,则您需要 8 GB 的内存.

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.

在 32 位系统上,这是不可能的.在 64 位系统上,您需要有足够的内存和交换空间才能工作.

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.

如果您还尝试在可执行文件的 .data 部分中静态创建数组,则该可执行文件最终可能有 8 GB 大,您可能会遇到文件系统限制(任何人都使用 fat32?).此外,编译器可能会因数据量而窒息(在 32 位上,它可能会崩溃).

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).

如果您在堆栈上分配(这是作为静态大小的局部变量数组),您还会在某些操作系统上遇到堆栈限制.

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.

这篇关于创建大量数字(10^9 大小)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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