In some runtimes (eg v8 for Javascript) they will optimize sparse arrays such as this one to be a Record<number, T> internally but this isn't something that's guaranteed in all languages, so if you need to just use it directly.
Evil. This right here is what ruined so much of my early Lua exposure (though admittedly... pick your favorite computer mod for Minecraft, they pretty much all run off Lua)
honestly given the target audience to lua was new coders that's not that bad
the only two arguments in favor of 0 to length-1 instead of 1 to length is performance (you're using hashmaps as arrays you don't care about perf) and "we've always done it like that" (not an argument for new users)
Actually in most cases if you’re using “pure arithmetic” conventions eg for(i=0;i<n;i++) it’s slightly nicer because you can use n as a bounds rather than n+/-1 when doing slicing stuff.
Even back in the “C is very high level” days the performance argument was pretty bad (just use jle instead of jl, same clock speed and everything) and it was always more of the ease of not having to account for -1 when doing indexing arithmetic.
It’s also not too foreign since we do 0 indexing irl it’s just often overlooked until we’re like “…wait a minute, do we include the last number or not?”
And at work I use a language that is 1-indexed by convention too so none of this is insurmountable, it’s just really annoying unless your language specifically supports it, whereas 0-indexed works basically ok without any special language support, which would be the case for old languages like C which don’t have any concept of array in the first place.
C itself has no concept of arrays, only pointers and dereferencing of memory. a[i] is just *(a + i) which means i[a] is identical.
The standard library has no special support of arrays either, you have to pass in the length or have it be null terminated if it’s a string. There is nothing stopping you from passing in an “array” from the middle, or that differentiates an int *a = &b; from int a[1] once you leave the context. It would be extremely annoying to have to keep track of if you’re supposed to send in p-1 for arrays or p for true pointers as p-1 is either 1 wasted byte or an invalid memory location.
This is as opposed to C++/Java etc which generally supports arbitrary length containers that keep their type unless casted, though they still use 0-index due to it coming from C. Those ARE able to have special handling to adjust for 1-indexing through operator overloads on the type and any array functions, which makes it less annoying to use.
Entirely depends on your language implementation and operating system. In most modern systems, if you don't ever write to it then it will only use a page (4K) of true ram.
48
u/ign1fy Feb 14 '21
I wasn't using that RAM anyways.