.NET is an endless supply of fascinating puzzles

September 19th, 2007 tonyg

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?

Entry Filed under: Technology, Rant

9 Comments Add your own

  • 1. Neil Bartlett  |  September 19th, 2007 at 2:41 pm

    Imaginary arrays…?

  • 2. tonyg  |  September 19th, 2007 at 6:10 pm

    Well that certainly sounds complex.

  • 3. Alex Blewitt  |  September 19th, 2007 at 7:11 pm

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

  • 4. matthew  |  September 20th, 2007 at 9:02 am

    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. tonyg  |  September 20th, 2007 at 12:01 pm

    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. Paul Crowley  |  September 20th, 2007 at 12:11 pm

    Being maximally generous I can only imagine something like this:

    o = new objectCache();

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

  • 7. tonyg  |  September 20th, 2007 at 12:36 pm

    Paul, you are indeed too kind.

  • 8. Rik Rose  |  October 5th, 2007 at 10:55 am

    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. tonyg  |  October 5th, 2007 at 11:58 am

    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.

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed

Calendar

September 2007
M T W T F S S
« Aug   Oct »
 12
3456789
10111213141516
17181920212223
24252627282930

Most Recent Posts