3 ms·
>Clearly, one of the three optimizations is incorrect in the sense that it introduced a change in program behavior. What about declaring that all three optimiz
by garaetjjte 2y ago
>Clearly, one of the three optimizations is incorrect in the sense that it introduced a change in program behavior.
What about declaring that all three optimizations are correct, and this program invokes UB? More precisely, it compares object one-past-the-end pointer with pointer pointing into entirely another object. one-past-the-end pointer is special because numerically it points outside the object. I think declaring that one-past-the-end pointer could be compared only with itself, valid pointer into the same object or NULL wouldn't be too unreasonable (comparing one-past-the-end pointer with pointer to another object would invoke UB).
- afdbcreid 2y agoBut it does not. It compares integers. Comparing integers is always well-defined. If you want to declare it UB, you will have to say that the integers are not simple bits, they also "remember" which pointer they came from. This is essentially what we mean when we say that integers has provenance, and it's not a good idea because it doesn't let you do some very common optimizations on integers.