3 ms·
> That would've already crashed at the p->some_field if p == NULL. 1. Typically yes, and that is exactly why the compiler can remove the check after it, it's a
by azakai 13y ago
> That would've already crashed at the p->some_field if p == NULL.
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.