数组的大小是否受 int 的上限限制(2147483647)? [英] Is the size of an array constrained by the upper limit of int (2147483647)?

查看:37
本文介绍了数组的大小是否受 int 的上限限制(2147483647)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些 Project Euler 练习,我遇到了一个场景,我 需要大于 2,147,483,647(C# 中 int 的上限)的数组.

I'm doing some Project Euler exercises and I've run into a scenario where I have want arrays which are larger than 2,147,483,647 (the upper limit of int in C#).

当然这些是大数组,但例如,我不能这样做

Sure these are large arrays, but for instance, I can't do this

// fails
bool[] BigArray = new BigArray[2147483648];

// also fails, cannot convert uint to int
ArrayList BigArrayList = new ArrayList(2147483648); 

那么,我可以有更大的数组吗?

So, can I have bigger arrays?

这是一个阿特金筛网,你知道的,所以我只想要一个非常大的:

It was for a Sieve of Atkin, you know, so I just wanted a really big one :D

推荐答案

任何时候处理这​​么大的数组时,您都应该尝试找到更好的解决方案.但话虽如此,我仍然会尝试回答你的问题.

Anytime you are working with an array this big, you should probably try to find a better solution to the problem. But that being said I'll still attempt to answer your question.

如本文章中所述.Net 中的任何对象都有 2 GB 的限制.适用于所有 x86、x64 和 IA64.

As mentioned in this article there is a 2 GB limit on any object in .Net. For all x86, x64 and IA64.

与 32 位 Windows 操作一样系统,有 2GB 的限制您可以同时创建的对象的大小运行 64 位托管应用程序在 64 位 Windows 操作系统上.

As with 32-bit Windows operating systems, there is a 2GB limit on the size of an object you can create while running a 64-bit managed application on a 64-bit Windows operating system.

此外,如果您在堆栈上定义了一个太大的数组,则会出现堆栈溢出.如果您在堆上定义数组,它将尝试将其全部分配到一个大的连续块中.最好使用在堆上具有隐式动态分配的 ArrayList.这不会让您超过 2GB,但可能会让您更接近它.

Also if you define an array too big on the stack, you will have a stack overflow. If you define the array on the heap, it will try to allocate it all in one big continuous block. It would be better to use an ArrayList which has implicit dynamic allocation on the heap. This will not allow you to get past the 2GB, but will probably allow you to get closer to it.

我认为只有当您使用 x64 或 IA64 架构和操作系统时,堆栈大小限制才会更大.使用 x64 或 IA64,您将拥有 64 位可分配内存,而不是 32 位.

I think the stack size limit will be bigger only if you are using an x64 or IA64 architecture and operating system. Using x64 or IA64 you will have 64-bit allocatable memory instead of 32-bit.

如果你不能一次分配所有的数组列表,你可以分部分分配.

If you are not able to allocate the array list all at once, you can probably allocate it in parts.

在具有 6GB RAM 的 x64 Windows 2008 机器上使用数组列表并一次添加 1 个对象,我最多可以获得 ArrayList 的大小:134217728.所以我真的认为您必须找到更好的解决方案您的问题没有使用尽可能多的内存.也许写入文件而不是使用 RAM.

Using an array list and adding 1 object at a time on an x64 Windows 2008 machine with 6GB of RAM, the most I can get the ArrayList to is size: 134217728. So I really think you have to find a better solution to your problem that does not use as much memory. Perhaps writing to a file instead of using RAM.

这篇关于数组的大小是否受 int 的上限限制(2147483647)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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