When can I check the value of a pointer

Well, I was recently surprised by this state of things: Evaluating a state containing a unified pointer is UB, but can it crash?

It looks like this:

int *p;
if(p != NULL)
{
  int k;
}

could theoretically end.

My question is: when can I check the value of a pointer? When can I check: if(ptr == SomeValue)- so that it does not call UB?

+4
source share
1 answer

It is safe to check the value of an initialized automatic variable. Pointers are no exception.

This is set out in section 6.3.2.1 and paragraph 2 in C.11:

, sizeof, &, ++, -- . , l, , , ( lvalue); lvalue conversion.... l , ( ), , ( ), .

+6

Source: https://habr.com/ru/post/1532255/


All Articles