3 ms·
I'm not sure this is correct. I think you are referring to the fact that if p was dereferenced before the NULL-check, then the compiler could optimize out the e
by xyzzy_plugh 11y ago
I'm not sure this is correct. I think you are referring to the fact that if p was dereferenced before the NULL-check, then the compiler could optimize out the entire conditional statement.
This is true as dereferencing a NULL pointer is undefined behaviour, so is fair game for optimizations. The compiler may assume that NULL pointer dereferencing results in a trap/segfault and that the conditional can never be executed.
If I am wrong, and your example actually exhibits undefined behaviour, I would certainly love to learn why.
- kibibu 11y agoNope, you're right - I meant to dereference the pointer, rather than just access it. D'oh!