Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

If C had 0-based pointers and 1-based indices, every CPU in existence would have a "subtract 1, multiply, and add" instead of an "multiply and add" instruction and everybody would think this is stupid because "multiply and add" could be used for a lot of things, but "subtract 1, multiply, and add" has only one use.


CPU instructions like load and move can apply an offset that is encoded as part of the instruction. This is more flexible than a special instruction for "offset=1", and useful beyond 1-based indexing. See for example https://www.cs.virginia.edu/~evans/cs216/guides/x86.html and https://www.keil.com/support/man/docs/armasm/armasm_dom13612... .

I'm not sure about "multiply and add"... Aren't these instructions for computing with floating point numbers rather than offsets?


Couldn't one based indexing be sugar the compiler provides?

I don't think having pointers and indices behave differently is a good idea for the record.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: