if (ptr == NULL)
{
...
}
...
int foo = ptr->some_field; if (ptr == NULL)
{
...
}
...
int foo = ptr->some_field;But this brings us back to the article: Why does the author say that there's no way to check for NULL in the free function? Maybe they are hinting at something completely unrelated to what we're saying here?
If that's where we failed to communicate, then that makes sense. Thanks, stranger!
In fact we can make the example even shorter: https://godbolt.org/z/8fv4GKMse
And here's one with a "time travelling" removal of a branch because of UB: https://godbolt.org/z/PjYzqKxs4
Remove the memcpy, and it adds the comparison back in: https://godbolt.org/z/r1GTvGnnf
This one is fun too: Explicitly crash on null pointer: https://godbolt.org/z/sGzhK7zM4
As this comment (https://news.ycombinator.com/item?id=45442103) says, it looks like we may have just been talking past each other.
When I said "If you check for null pointer before you dereference, then no the compiler cannot elide the check." I meant like:
if (ptr != NULL) { *ptr = 123; }
Which is obviously safe and the compiler cannot remove the check. Not: if (ptr != NULL) { [… something else …] } *ptr = 123;
The latter is UB, and UB is allowed to time travel.