technology from back to front

.NET is an endless supply of fascinating puzzles

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 ints, an array’s length field is also signed. What could it possibly mean to have a length less than zero?

by
tonyg
on
19/09/07
  1. Imaginary arrays…?

  2. Well that certainly sounds complex.

  3. It’s so that there’s backward compatibility for all those buffer underflows that viruses have come to rely on

  4. 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…

  5. 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.

  6. Being maximally generous I can only imagine something like this:

    o = new objectCache();

    o.maxArraySize = -1; // Disallow all array creation

  7. Paul, you are indeed too kind.

  8. 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.

  9. 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.

 
 


× one = 8

2000-14 LShift Ltd, 1st Floor, Hoxton Point, 6 Rufus Street, London, N1 6PE, UK+44 (0)20 7729 7060   Contact us