3 ms·
Wait, the compiler can do that? I thought only dereferencing a null pointer gives undefined behavior, just checking if a pointer is 0 or not shold be valid? Ye
by krackers 4y ago
Wait, the compiler can do that? I thought only dereferencing a null pointer gives undefined behavior, just checking if a pointer is 0 or not shold be valid?
Yeah based on https://joelaro.wordpress.com/2015/09/30/gcc-optimization-fdelete-null-pointer-checks/ https://joelaro.wordpress.com/2015/09/30/gcc-optimization-fd... the assumption is that if the pointer was previously dereferenced, it can be assumed to be non-null, and hence all subsequent null-checks can be elided.
Based on https://news.ycombinator.com/item?id=17360772 https://news.ycombinator.com/item?id=17360772, the case in which this actually makes a difference is an address offset like
int *n = ¶m->length
The compiler knows the structure so it can just inline the offset, but the optimizer considers this as being non-null.
- 3836293648 4y agoYou can check if the address is 0, but the value of the nullptr is implementation definied behaviour. You can have a system where the null pointer is 4. And address 0 isn't too special on a machine level either. Embedded systems have valid data to read and write there.
- deleted 4y ago[deleted]