.NET is an endless supply of fascinating puzzles
This page is a mirrored copy of an article originally posted on the (now sadly defunct) LShift blog; see the archive index here.
Wed, 19 September 2007
In C, size_t
is unsigned. In Java, there are no unsigned fixed-width pseudointegral types, so it can perhaps be forgiven for having an array’s length field be signed. In .NET, however, which has unsigned int
s, an array’s length field is also signed. What could it possibly mean to have a length less than zero?
Comments
On 19 September, 2007 at 2:41 pm,
wrote:On 19 September, 2007 at 6:10 pm,
wrote:Well that certainly sounds complex.
On 19 September, 2007 at 7:11 pm,
wrote:It’s so that there’s backward compatibility for all those buffer underflows that viruses have come to rely on
On 20 September, 2007 at 9:02 am,
wrote:Hang on, if you follow this route too far then you’ll end up wanting different sizes of ints depending on the amount of memory available at runtime - after all, if you’ve only got 32MB of RAM available then the type of the number should not allow values greater than 33554432. But this should also take into account the size of the values in the array - if they’re just bools and they’re bit-packed, then 33554432 won’t be too bad. But if they’re large objects, then a limit rather lower is needed…
On 20 September, 2007 at 12:01 pm,
wrote:Matthew, that’s an excellent point! If I were being serious, I’d say something along the lines of “clearly choosing a fixed representation for array lengths (and in fact arrays) is a case of premature optimisation”… but since I’m not, I’ll suggest that what C# really needs is a dependent type system.
On 20 September, 2007 at 12:11 pm,
wrote:Being maximally generous I can only imagine something like this:
o = new objectCache();
o.maxArraySize = -1; // Disallow all array creation
On 20 September, 2007 at 12:36 pm,
wrote:Paul, you are indeed too kind.
On 5 October, 2007 at 10:55 am,
wrote:There’s several views this could take, on first reading. Not knowing C# yet, I must confess that I don’t know the actual answer.
Given a negative index, there are two sensible things that I can see the langauge doing.
Firstly, it could perform a bounds check on 0 <= index <= sizeof(array), and throwing an error if it’s out of bounds.
Secondly, it could perform a bounds check on abs(index) <= sizeof(array), and then work backwards from the end, if index < 0. This is what Ruby does.
I would hazard a guess that option 2 would not have been seen to be as helpful as possible, and so option 1 is what I would expect to happen.
On 5 October, 2007 at 11:58 am,
wrote:Rik, I’m not talking about indexes - I’m talking about the array size itself. Also, the bounds check would be 0 <= index < sizeof(array), rather than 0 <= index <= sizeof(array). An array of size zero is empty, and indexing it at all is an error.
Imaginary arrays…?