On Wednesday, 8 June 2022 at 16:32:25 UTC, John Colvin wrote:
> >There is no frame of reference in which this result is in any way reasonable.
My guess is that technically foo
has undefined behaviour.
Sure, but that also mean it could format your hard drive, and it'd be hard to argue this is reasonable.
If the compiler understands of what's going on to decide it can recycle the memory, it understands enough to tell you you are using it after freeing and if it cannot, then it shouldn't do it.
In this case specifically, assuming the compiler see the memory doesn't escape and promoting [c] on stack, it should still do the right thing. That means the compiler is somehow getting out of its way to break the code.
That doesn't sound reasonable, no matter how you slice it.