Summary
A collection of Andrew Koenig’s “Much Ado About Nothing” topics on Drdobbs.
Pointer Arithmetic: A Major (Dis)advantage of C and C++
The gist: referencing an array by indexes and by pointers are equivalent in C, but when using pointer reference, it hard to check array bounds. That’s why we use off-the-end indexing. because an off-the-end index can ensure the correctness when translating the same code from indexing to pointer or vice versa.
Much Ado About Nothing: Why Off-The-End Pointers Are Necessary
The gist: we usually prefer to design things in a symmetric way, like iterate an array forward and backward, but without the off-the-end pointer, it needs a special process, and the code is ugly. it is especially useful in handing an empty array while processing each of the element in the array.
Still More Ado About Nothing: How Off-The-End Pointers Should Behave
The gist: there is no need to have the second off-the-end pointer because they are not adding additional advantages.
If Off-The-End Pointers Are Useful, Why Not Off-The-Beginning Pointers?
The gist: it simply doesn’t make things cleaner, but messier.
Off-The-End Pointers And While Statements
The gist: the asymmetric feature of off-the-end pointer complies with the while statement in that the condition is false after the loop terminate.
Using while statements changes one’s perspective subtly from using for or DO statements, because when we write a while statement, instead of stating the last value that a variable should have within a loop, we state explicitly a condition that we expect to be false after the loop finishes.
In other words, using off-the-end pointers is a natural outgrowth of shifting from ranges defined explicitly by their first and last elements to ranges defined by while statements. In such statements, the loop ends when the condition is false for the first time, not when it is true for the last time. As a result, there is a natural asymmetry about using while statements that maps into an asymmetry in the values that those while statements manipulate.