dereferencing the null pointer

前端 未结 4 2293
灰色年华
灰色年华 2020-11-28 09:35
int* p = 0;
int* q = &*p;

Is this undefined behavior or not? I browsed some related questions, but this specific aspect didn\'t show up.

4条回答
  •  佛祖请我去吃肉
    2020-11-28 10:04

    Yes, dereferencing the null pointer is undefined behavior. Integer constant 0 in a pointer context is the null pointer. That's it.

    Now, if your second line was int *q = p; that would be a simple pointer assignment. If the compiler removes the &* and reduces the dereference to an assignment, you're OK.

提交回复
热议问题