3 ms·
> since the first element of an array is zero distance away from the beginning of the array. That's much less of a win in any language but C, where "array" mea
by ArbitraryLimits 13y ago
> since the first element of an array is zero distance away from the beginning of the array.
That's much less of a win in any language but C, where "array" means something more than "pointer." If you're checking array bounds on every access then the extra assembler instruction to subtract one from your index doesn't matter in comparison, whereas if you're just dereferencing a pointer it could potentially double the cost of array accesses (two instructions instead of one if your instruction set has base + offset addressing modes).
- dpratt 13y agoI'm just thinking logically - not even on a pointer level. If I'm doing index/offset math to calculate an array position, it's foolish to start at 1 instead of 0.
- anonymoushn 13y agoAdding things that aren't conceptually indices to indices works the same way in both conventions (so a[idx] and a[idx+k] have k-1 things in between). You miss out on stuffing multiple dimensions into one with a[x*xlen+y], but there's generally no reason to do that. Could you elaborate about the uses you have in mind?.