What an annoying post. Talks like he knows a lot, when he doesn't know this most basic of basic points of how C works. It's understandable to not know everything, even to have the odd hole in your basics like this. But the know-it-all voice (and for such a basic topic) "I explained operator precedence..." is just going to make you someone nobody wants to work with. This is not undefined behavior, it's one of the firs…
A glimpse of undefined behavior in C
61–67 of 67 posts
Re: A glimpse of undefined behavior in C
#62What an annoying post. Talks like he knows a lot, when he doesn't know this most basic of basic points of how C works. It's understandable to not know everything, even to have the odd hole in your basics like this. But the know-it-all voice (and for such a basic topic) "I explained operator precedence..." is just going to make you someone nobody wants to work with. This is not undefined behavior, it's one of the firs…
What exactly is not undefined behavior? The post describes a statement where a variable is modified multiple times between sequence points. What does the standard say about this?
N1256, 6.5 says
Between the previous and next sequence point an object
shall have its stored value modified at most once by the
valuation of an expression.
J.2 Undefined behavior The behavior is undefined in the following circumstances:
[..]
* Between two sequence points, an object is modified more
than once, or is modified and the prior value is read
other than to determine the value to be stored (6.5).
What's with the know-it-all voice?Re: A glimpse of undefined behavior in C
#63Isn't this actually called "implementation-defined" behaviour in the std rather than "undefined" behaviour? I generally get a segfault for undefined behaviors
It's undefined. That means the compiler and runtime can choose to segfault, do something that looks right, silently corrupt your program, launch the nuclear missiles, whatever. And there's no requirement that this behavior be repeatable, so the code can work with debug flags turned on but fail in a release build. You can never rely on your compiler's implementation of undefined behavior.
I wish I could somehow get my coworker to understand this. He's mostly "Yeah, well, I know it's undefined, but there's really no way this could be anything else than . I know what the CPU does there."
Re: A glimpse of undefined behavior in C
#64Aside: this test code would have been a bit easier to debug if it had used decimal bases. That is, replace int a[] = {10,20,30}; int r = 1 * a[i++] + 2 * a[i++] + 3 * a[i++]; with int a[] = {1,2,3}; int r = 1 * a[i++] + 10 * a[i++] + 100 * a[i++]; Then if your program outputs r = 111, it's obvious that it's doing a[0] + 10*a[0] + 100*a[0] and if it outputs 321, it's obvious that it's doing a[0] + 10*a[1] + 100*a[2] N…
Re: A glimpse of undefined behavior in C
#65Interesting, my first guess would have been '60' since the old C89 / K&R C had the behavior (prefix ops) => ( statement ) =>(postfix ops) Which made code like *a++ = *b--; Change the pointers after the copy as opposed to having them change before the copy. It is interesting that this has become compiler defined.
Re: A glimpse of undefined behavior in C
#66Earlier quoted context omitted.
It hasn't. You're not modifying a single variable twice within a single sequence point in that example.
That's really all there is to it, isn't it? I've never understood why some people feel the urge to modify the same variable multiple times in the same statement. It doesn't help readability, it's harder to reason about and worst of all leads you directly into the land of undefined behavior, where everything that can go wrong, will go wrong.
Re: A glimpse of undefined behavior in C
#67Earlier quoted context omitted.
That's really all there is to it, isn't it? I've never understood why some people feel the urge to modify the same variable multiple times in the same statement. It doesn't help readability, it's harder to reason about and worst of all leads you directly into the land of undefined behavior, where everything that can go wrong, will go wrong.
Yeah, the need to shove as much code as possible into one line seems to go away with experience. Once you have to debug and maintain that crap you realize very quickly that one liners are not necessarily something to strive for.
That's something that took me way too long to learn and I feel that novice programmers should embrace that more. We all like to feel like Einstein sometimes, but a big code base is not the place for solutions only we can understand... temporarily.