3 ms·
It wouldn't make it safe; it would make it so that the consequences of returning that pointer are just the same as if you had written an assembler routine retur
by kps 9y ago
It wouldn't make it safe; it would make it so that the consequences of returning that pointer are just the same as if you had written an assembler routine returning that pointer. On all common hardware, that would be none if the pointer is never used; on most common hardware, it would be none if the pointer is never used in a call at least equally deep — but anyway, you'd get whatever the machine does, and if you don't like it, that's your problem. In pre-UB C, the ‘spirit of C’ that C89 was intended to maintain was that you get what you wrote.
By contrast, modern ‘undefined behaviour’ means that if you return that pointer — even if it is never actually used — the compiler can throw away all the code leading up to that point (including external side effects) on the grounds that it “can't happen”. You get much less than what you wrote.
- bonzini 9y agoAre you sure about that? Can you point to a clause in the standard? What may certainly cause the behavior you describe, though, is creating a pointer to an out-of-bounds element of an array.