News for hackers. Stuff that matters.
Re:On the history and justification of the C programming lan... by pcwalton on Thursday August 18, 2016 @01:19PM (#12314446)
> The undefined behavior is there primarily to account for the variation in platforms. Higher-level languages avoid this by hiding it in a platform-specific compiler or VM, which in turn limits the number of platforms the code can actually be run on.

It may have started this way in the committee (though that isn't true for e.g. type based alias rules), but nowadays it's depended on for performance. You lose a lot of ability to reason about loop trip count if signed overflow is defined, for example.

> Citation needed. I get that undefined behavior is hard to spot, but having spent years writing cross-platform C I cannot accept that every large program depends on it without some evidence.

The probability that a program depends on any of (nonexhaustive list) (a) inspecting the value of a dangling pointer; (b) taking the offset from NULL; (c) type-punning through a union; (d) type-punning through any type other than char; (e) signed overflow; (f) order of operations between sequence points quickly approaches 1.

Here's a very interesting survey investigating this: https://www.cl.cam.ac.uk/~pes20/cerberus/notes50-survey-disc...

Re:On the history and justification of the C programming lan... by vonmoltke on Thursday August 18, 2016 @01:44PM (#12314706)
> It may have started this way in the committee (though that isn't true for e.g. type based alias rules), but nowadays it's depended on for performance. You lose a lot of ability to reason about loop trip count if signed overflow is defined, for example.

I agree it is depended on in many cases, and should not be. That is on the programmers though, not the language designers. The behavior does not exist to cover deficiencies in the language but those of its users.

> The probability that a program depends on any of (nonexhaustive list) (a) inspecting the value of a dangling pointer; (b) taking the offset from NULL; (c) type-punning through a union; (d) type-punning through any type other than char; (e) signed overflow; (f) order of operations between sequence points quickly approaches 1.

Just on your short list I can tell you the probably of any of our C code depending on that approached zero:

> inspecting the value of a dangling pointer

Our code was malloc once, free never. Dangling pointers were by definition impossible.

> taking the offset from NULL > type-punning through a union > type-punning through any type other than char > signed overflow > order of operations between sequence points

Our coding standards covered all of those, so their existence would be a code review failure.

We weren't specifically writing to MISRA C, but I will wager that your statement is not accurate for any project that follows it or any similar standard. Yes, it is a lot of work that you don't need to do with a more controlled and less flexible language. I'm not going to challenge that. I'm just challenging the idea that nobody is writing C that avoids undefined behavior.

Re:On the history and justification of the C programming lan... by catnaroek on Thursday August 18, 2016 @01:52PM (#12314764)
> Our code was malloc once, free never. Dangling pointers were by definition impossible.

Dangling pointers can arise in other ways: use an uninitialized pointer variable, take the wrong offset from an otherwise valid pointer, etc.

Re:On the history and justification of the C programming lan... by vonmoltke on Thursday August 18, 2016 @02:09PM (#12314951)
Those are indeterminate values but they aren't dangling pointers. Dangling pointers are specifically references to an object that has been freed.

Plus, the cases you highlighted are programming errors, not reliance on undefined behavior.

Re:On the history and justification of the C programming lan... by hawski on Friday August 19, 2016 @06:07AM (#12319005)
(a) how program can depend on inspecting the value of a dangling pointer? I can't think of it as not a bug.

(b) True. I think this one should be defined and standardized.

(c) It seems that C99 defined this behaviour. https://stackoverflow.com/questions/11639947/is-type-punning...

(d) I am not knowledgeable enough. Could you give an example?