Is incrementing a null pointer well-defined?

后端 未结 9 472
情话喂你
情话喂你 2020-12-03 04:17

There are lots of examples of undefined/unspecified behavior when doing pointer arithmetics - pointers have to point inside the same array (or one past the end), or inside t

9条回答
  •  佛祖请我去吃肉
    2020-12-03 05:06

    It turns out it's actually undefined. There are systems for which this is true

    int *p = NULL;
    if (*(int *)&p == 0xFFFF)
    

    Therefore, ++p would trip the undefined overflow rule (turns out that sizeof(int *) == 2)). Pointers aren't guaranteed to be unsigned integers so the unsigned wrap rule doesn't apply.

提交回复
热议问题