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

I quite disagree, I think it makes two insightful points:

1. In this discussion, people often overlook that these numbers are applied to two different concepts: offsets and numbering.

2. If C had been designed with 0-based pointers (for offset calculation) and 1-based indices (for numbering the array elements) people would probably find this a strength, to have the most appropriate base for each case. It's an interesting what-if idea that I hadn't seen before.



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: