Array Indices: Start from 0 or 1?(xkcd.com)
xkcd.com
Array Indices: Start from 0 or 1?
http://xkcd.com/163/
13 comments
There was lots of Visual Basic code written in the 90s. Not all of it has been rewritten.
Was it thrown away?
Is it... still running? {o_O}
Was it thrown away?
Is it... still running? {o_O}
Imagine the problem that certain companies (who have delayed investing in their products) are running into now that Microsoft has announced no VB runtime will be in Windows 7 (optimistically due in early 2010).
I hope they're starting their migration/risk mitigation strategies now.
I hope they're starting their migration/risk mitigation strategies now.
I don't understand what they mean by "no VB runtime". My impression was that the VB installer always included all the DLLs necessary to run on a generic Win32 system. If they change something to break VB programs, they'll also be breaking lots of other Win32 software, too.
Those are what FORTRAN people are hired for. To maintain their archaic systems which store vital information.
Sounds like a business opportunity to me.
Sadly my compromise of using 0.5 was flat out rejected.
1-based arrays always make me think of the Numerical Recipes books, specifically the one using C. They jump through all sorts of hoops to have 1-based arrays:
- Array allocations are done using helper macros which invoke malloc and return a pointer to memory one element before the beginning of the array. (I'm pretty sure this is undefined behaviour according to the standard btw; it just happens that the format of pointers isn't data-dependent anymore these days)
- Array accesses are just as usual in C, except array[1] is the first element.
- Freeing memory is again wrapped in a macro to calculate the actual beginning of the array.
- Multidimensional arrays are layers of this crap.
- Array allocations are done using helper macros which invoke malloc and return a pointer to memory one element before the beginning of the array. (I'm pretty sure this is undefined behaviour according to the standard btw; it just happens that the format of pointers isn't data-dependent anymore these days)
- Array accesses are just as usual in C, except array[1] is the first element.
- Freeing memory is again wrapped in a macro to calculate the actual beginning of the array.
- Multidimensional arrays are layers of this crap.
This is because most of the most important numerical libraries (e.g. LINPACK, EISPACK, LAPACK) are written in FORTRAN. There's (literally) decades of work in these libraries, and everyone uses them, and it's simply not worth trying to rewrite them in whatever the language du jour is.
I realise that, although I'd have to say it's not exactly the way I would have handled the situation. In my experience, linking binary Fortran libraries against C code doesn't lead to interfaces that expect such array pointers, and when porting Fortran code to C, I would have probably handled the index numbering issue with a macro for element access where converting to 0-based notation isn't completely foolproof.
Actually, I probably would have provided the numerical recipes code in two versions, one in the 1-based format, the other in idiomatic C.
But then I'm more of a software engineer than a computational physicist even though my degree certificate claims otherwise.
Actually, I probably would have provided the numerical recipes code in two versions, one in the 1-based format, the other in idiomatic C.
But then I'm more of a software engineer than a computational physicist even though my degree certificate claims otherwise.
I think it would have been more appropriate to have linked directly to the XKCD comic in question instead of to blog spam.
Sorry, I can't seem to change the url. Here's the original url http://xkcd.com/163/.
I grew up thinking all array indices started at 0. That belief was held up until I met Lua of course.
What is your quest? To conquer the world with Visual Basic.
At what index shall arrays start? One... no, zero... (arghhhhhhh.....)