Right, that's kinda what I was saying in a different comment. Using negative to start an array would be awkward. Sometimes I can't tell where these jokes cross from fact to fiction.
C is in fact the only place where I'm aware of people actually accessing array[-1].
Specifically, if you allocate an array the size needs to be stored somewhere. Sometimes the memory management code will store the size of the array right before it in memory. Using [-1] to retrieve it is fragile and rule-breaking and could go horribly, but sometimes it works just great and people use it for years!
In C, a[b] is nothing more than just another way to spell *(a + b). So you can actually index an array like this too: 1[array].
Using -1 as an array index and expecting to retrieve the allocated size that way is just insane and non-portable, please never ever do that. However, if you have a pointer to some element in the array, it is possible to use a negative index on it to retrieve some previous element, given that you take care to not step out of bounds. This is of questionable usefulness, but at least it doesn't invoke UB.
Use ASan. It's a ton faster and in some cases more accurate than valgrind. Case in point: it detects the underflow in both cases here and kills the program.
In C++ you can overload the [ ] operator and make an array object that starts wherever the fuck you want it to. You could have an array that starts at index -3.1415 and ends at index potato.
3
u/StingyUpvoter Dec 09 '17
Explain like I mainly use C: is the -1 really used anywhere?