|
|
|
|
|
by fstd
3452 days ago
|
|
if P does not point to valid memory, then dereferencing it is UB. Therefore, the compiler is at liberty to do whatever it wants (whether or not that is a good idea is irrelevant for the purposes of this consideration). If the compiler knows that this is undefined, then you have already broken your end of the deal and therefore the compiler is free to break their end of the deal. Therefore, eliminating the line is completely okay and the only think broken there is the code. |
|
IIRC, simply making P point to anything other than an object or 1 past the end of an array is already UB. I suspect that this is because some architectures have dedicated address registers and can take an exception when an invalid address is loaded/calculated instead of doing it when the memory access happens.