如何获取索引大于max_int的数组元素?

时间:2022-11-01 21:49:12

Let's say we have a string which its length is very big, let's say even bigger than max_int.

假设我们有一个字符串它的长度很大,甚至大于max_int。

string str="this should contain a long string";

if I want to reach to the str[100000000000].

如果我想要到达str[100000000000]。

How should I do it?

我该怎么做呢?

When I try to put an index which its type isn't int I get the following error:

当我试图放一个索引,它的类型不是int,我得到以下错误:

The best overloaded method match for 'string.this[int]' has some invalid arguments

最佳重载方法匹配“string”。这个[int]有一些无效的参数

4 个解决方案

#1


2  

The maximal size of a single object was 2GB prior .NET 4.5, so you could never have a string that big, so there was no purpose in trying to use an index this large.

单个对象的最大大小在。net 4.5之前是2GB,所以不能有这么大的字符串,所以尝试使用这么大的索引是没有意义的。


In .NET 4.5, you can increase that limit. Quote from MSDN:

在。net 4.5中,可以增加这个限制。从MSDN报价:

"By default, when you run a 64-bit managed application on a 64-bit Windows operating system, you can create an object of no more than 2 gigabytes (GB). However, in the .NET Framework 4.5, you can increase this limit. For more information, see the gcAllowVeryLargeObjects element."

默认情况下,当您在64位Windows操作系统上运行一个64位托管应用程序时,您可以创建一个不超过2g (GB)的对象。但是,在。net Framework 4.5中,您可以增加这个限制。有关更多信息,请参见gcAllowVeryLargeObjects元素。

However, most containers still have interfaces based on int and even for arrays this won't help...

然而,大多数容器仍然有基于int的接口,甚至对于数组来说这也没用。

"The maximum number of elements in an array is UInt32MaxValue."

“数组中元素的最大数量是UInt32MaxValue。”

...which is 4,294,967,295 which is still smaller than 100,000,000,000 you proposed.

…也就是4,294,967,295仍然小于你提出的一亿。


Do you really need to have that much continuous memory? Why not split your data to smaller chunks?

你真的需要有那么多连续的记忆吗?为什么不把数据分割成更小的块呢?

#2


2  

You can't. I don't believe you can have an object that takes more than 2^32 bytes anyway, so you really won't run into this.

你不能。我不相信你能有一个对象,需要超过2 ^ 32个字节,所以你真的不会遇到这个问题。

According to this post the maximum CLR object size is 2GB, which affirms my statement.

根据这篇文章,CLR对象的最大大小是2GB,这证实了我的说法。

#3


1  

You need to implement custom array that supports large range of indexes.

您需要实现支持大范围索引的自定义数组。

All normal .Net types (like String, Array, List) support only integers for indexing. This is partially due to restriction on maximum continuous allocation size - so your custom class need to chunk data to support such indexes.

所有普通的。net类型(如字符串、数组、列表)只支持用于索引的整数。这部分是由于对最大持续分配大小的限制——因此您的自定义类需要大量数据来支持这些索引。

#4


1  

You can use Array.GetValue to get index of array that larger than int.

您可以使用数组。GetValue获取大于int的数组索引。

#1


2  

The maximal size of a single object was 2GB prior .NET 4.5, so you could never have a string that big, so there was no purpose in trying to use an index this large.

单个对象的最大大小在。net 4.5之前是2GB,所以不能有这么大的字符串,所以尝试使用这么大的索引是没有意义的。


In .NET 4.5, you can increase that limit. Quote from MSDN:

在。net 4.5中,可以增加这个限制。从MSDN报价:

"By default, when you run a 64-bit managed application on a 64-bit Windows operating system, you can create an object of no more than 2 gigabytes (GB). However, in the .NET Framework 4.5, you can increase this limit. For more information, see the gcAllowVeryLargeObjects element."

默认情况下,当您在64位Windows操作系统上运行一个64位托管应用程序时,您可以创建一个不超过2g (GB)的对象。但是,在。net Framework 4.5中,您可以增加这个限制。有关更多信息,请参见gcAllowVeryLargeObjects元素。

However, most containers still have interfaces based on int and even for arrays this won't help...

然而,大多数容器仍然有基于int的接口,甚至对于数组来说这也没用。

"The maximum number of elements in an array is UInt32MaxValue."

“数组中元素的最大数量是UInt32MaxValue。”

...which is 4,294,967,295 which is still smaller than 100,000,000,000 you proposed.

…也就是4,294,967,295仍然小于你提出的一亿。


Do you really need to have that much continuous memory? Why not split your data to smaller chunks?

你真的需要有那么多连续的记忆吗?为什么不把数据分割成更小的块呢?

#2


2  

You can't. I don't believe you can have an object that takes more than 2^32 bytes anyway, so you really won't run into this.

你不能。我不相信你能有一个对象,需要超过2 ^ 32个字节,所以你真的不会遇到这个问题。

According to this post the maximum CLR object size is 2GB, which affirms my statement.

根据这篇文章,CLR对象的最大大小是2GB,这证实了我的说法。

#3


1  

You need to implement custom array that supports large range of indexes.

您需要实现支持大范围索引的自定义数组。

All normal .Net types (like String, Array, List) support only integers for indexing. This is partially due to restriction on maximum continuous allocation size - so your custom class need to chunk data to support such indexes.

所有普通的。net类型(如字符串、数组、列表)只支持用于索引的整数。这部分是由于对最大持续分配大小的限制——因此您的自定义类需要大量数据来支持这些索引。

#4


1  

You can use Array.GetValue to get index of array that larger than int.

您可以使用数组。GetValue获取大于int的数组索引。