Is storing an invalid pointer automatically undefined behavior?

前端 未结 7 1409
谎友^
谎友^ 2020-11-27 06:52

Obviously, dereferencing an invalid pointer causes undefined behavior. But what about simply storing an invalid memory address in a pointer variable?

Consi

7条回答
  •  一整个雨季
    2020-11-27 07:31

    Your code is undefined behavior for a different reason:

    the expression begin - 1 does not yield an invalid pointer. It is undefined behavior. You are not allowed to perform pointer arithmetics beyond the bounds of the array you're working on. So it is the subtraction itself that is invalid, and not the act of storing the resulting pointer.

提交回复
热议问题