On 1/5/2014 4:20 PM, deadalnix wrote:
On Monday, 6 January 2014 at 00:13:19 UTC, Walter Bright wrote:
I'd still like to see an example, even a contrived one.

void foo(int* ptr) {
     *ptr;
     if (ptr is null) {
         // do stuff
     }

     // do stuff.
}

The code look stupid, but this is quite common after a first pass of
optimization/inlining, do end up with something like that when a null check if
forgotten.

The code is fundamentally broken. I don't know of any legitimate optimization transforms that would move a dereference from after a null check to before, so I suspect the code was broken before that first pass of optimization/inlining.


The problem here is that the if can be removed, as you can't reach that point if
the pointer is null, but *ptr can also be removed later as it is a dead load.

The resulting code won't crash and do random shit instead.

If you're writing code where you expect undefined behavior to cause a crash, then that code has faulty assumptions.

This is why many languages work to eliminate undefined behavior - but still, as a professional programmer, you should not be relying on undefined behavior, and it is not the optimizer's fault if you did. If you deliberately rely on UB (and I do on occasion) then you should be prepared to take your lumps if the compiler changes.

Reply via email to