> the compiler is free to assume that `p != NULL` is always true
Only until the first point in the "..." part where it cannot prove that p has not been modified.
> the compiler is free to assume that `p != NULL` is always true
Only until the first point in the "..." part where it cannot prove that p has not been modified.
http://blog.llvm.org/2011/05/what-every-c-programmer-should-...
Please read that article, and the rest in the series. Undefined behavior is far more pervasive than you think.
This and the Apple SSL bug makes me think that optimising compilers should be far more explicit (i.e. emitting messages or even warnings) about what they're doing than they are now, because it seems far too much about the optimisation process is being hidden and not transparent enough. Not only unreachable code, messages like "result of computation x is never used", "if-condition assumed to be {false, true}", "while-loop condition always false - body removed", etc. would be extremely useful for detecting and fixing these problems.
1. Typically yes, and that is exactly why the compiler can remove the check after it, it's already undefined behavior so it can assume the optimistic case (and remove the check).
2. But it is not always true that it would crash. p->some_field might happen to be in a valid memory location to be accessed. This doesn't happen normally because low memory addresses (0-1024, say) tend to not be accessible by userspace programs. But I am not aware of a spec ensuring that. An OS could in theory let your program map memory at address 4, in which case p->some_field would succeed if p is NULL and the offset if 4.