Re: [perl #30979] Array indices wrap around at 2**32
by John Peacock other posts by this author
Aug 16 2004 3:52PM messages near this date
[perl #30979] Array indices wrap around at 2**32
|
[perl #30978] "make test" op/write failure
Andy Lyttle (via RT) wrote:
> Attempting to access an array element using a very large value for
> the index yields weird results, particularly near [2**32]. See this
> test case:
This is a Very Bad Idea(TM) in general; attempting to access array
elements past the end of the current array causes all intervening
elements to spring into existance, along with the [in this case huge]
memory allocation required to do so.
With that in mind, Perl uses integer offsets for arrays (just as C does,
since Perl is written in C), which means that you are limited to 2**32-1
(unless you have 64bit integers). And it also means that you have to be
careful not to wrap the offset around.
> Expected behavior is to return undef and warn "Use of uninitialized
> value", and not wrap around at all, because that's the sort of weird
> behavior we expect from C, not from perl.
There is no way to (in the general case) know when the user really meant
a really big positive offset, instead of an equally big negative offset
(since that is a legitimate array lookup too). There is nothing that
could be undef, and hence no way to trap this error.
HTH
John
--
John Peacock
Director of Information Research and Technology
Rowman & Littlefield Publishing Group
4501 Forbes Boulevard
Suite H
Lanham, MD 20706
301-459-3366 x.5010
fax 301-429-5748
Thread:
Andy Lyttle (via RT)
John Peacock
|