What is the rationale for limitations on pointer arithmetic or comparison?

后端 未结 7 1756
半阙折子戏
半阙折子戏 2020-12-03 01:30

In C/C++, addition or subtraction on pointer is defined only if the resulting pointer lies within the original pointed complete object. Moreover, comparison of two pointers

7条回答
  •  甜味超标
    2020-12-03 01:55

    There are architectures where program and data spaces are separated, and it's simply impossible to subtract two arbitrary pointers. A pointer to a function or to const static data will be in a completely different address space than a normal variable.

    Even if you arbitrarily supplied a ranking between different address spaces, there's a possibility that the diff_t type would need to be a larger size. And the process of comparing or subtracting two pointers would be greatly complicated. That's a bad idea in a language that is designed for speed.

提交回复
热议问题