在Delphi中动态数组的最大长度? [英] Max length for a dynamic array in Delphi?

查看:478
本文介绍了在Delphi中动态数组的最大长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇一个动态数组可能要等多久,所以我尝试了

I was curious how long a dynamic array could be so I tried

SetLength(dynArray, High(Int64));

该值为9,223,372,036,854,775,807,我认为这将是我可以引用的最大索引数。它给了我一个答案:

That has a value of 9,223,372,036,854,775,807 and I figure that would be the largest number of indexes I could reference anyway. It gave me a:


ERangeError并显示消息范围检查错误。

ERangeError with message 'Range check error'.

所以我尝试了:

SetLength(dynArray, MaxInt); 

,并得到相同的错误!

有趣的是,我可以这样称呼它

Interestingly I could call it with

SetLength(dynArray, Trunc(Power(2, 32));

实际上是MaxInt大小的两倍!

Which is actually twice the size of MaxInt!

我尝试过

SetLength(dynArray, Trunc(Power(2, 63) - 1));

与High(Int64)相同,但也失败了。

Which is the same as High(Int64), and that failed too.

短反复试验的结果,有人知道最大大小吗?这取决于数组中元素的大小吗?

Short of continued trial and error, does someone know the maximum size? Does it depend on the size of the elements in the array?

我正在使用Delphi 2009。对于不同的版本来说是不同的(显然,当Commadore出现时,它应该更大!)

I am using Delphi 2009. Will it be different for different versions (obviously when Commadore comes out it should be greater!)

推荐答案

答案从System.DynArraySetLength过程中很明显,从第20628行开始:

The answer is clear from System.DynArraySetLength procedure, from line 20628:

Inc(neededSize, Sizeof(Longint)*2);
if neededSize < 0 then
  Error(reRangeError);

因此,在不引起范围检查错误的情况下可以分配的最大值理论上是Maxint-SizeOf(Longint)*2。实际上,您将获得内存不足的情况错误取决于可用的内存量。

The maximum value you can allocate without raising a range check error is therefore theoretically Maxint - SizeOf(Longint) * 2. Practically, you will get an out-of-memory error depending on how much memory is available.

这篇关于在Delphi中动态数组的最大长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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