2

In a large project compiled using latest g++, following behaviour at "--->" is consistently observed:

void somewhere ()
{
  T* pt = find(x); // returns `nullptr` for `x`
  foo(*pt);
}

void foo (T& t)  // many times `t` is created from `pt = nullptr`
{
  if(condition)
    t.set(0);  // ---> But app crashes, only if this is touched
}

Means, find(x) always returns nullptr to pt. But the app doesn't crash, unless the condition is true. Suppose, if the condition is never true when pt was nullptr, is the app still in the UB state?

In other words as marked in question:
Is dereferencing a NULL pointer into a reference, which is never accessed for read/write, still an undefined behaviour?

As null pointer itself is not a UB, but only accessing it. Similarly, the reference derived from such pointer (analogically a "null reference") shouldn't act in the same way!

iammilind
  • 68,093
  • 33
  • 169
  • 336

0 Answers0