News for hackers. Stuff that matters.
Re:Everything in C is undefined behavior by poppadom1982 on Wednesday May 20, 2026 @08:43AM (#48206771)
You're missing the point. Volatile forces two loads of a value that may have changed in the middle. So the value of "x" may depend on the time/order of load.
Re:Everything in C is undefined behavior by AnimalMuppet on Wednesday May 20, 2026 @09:17AM (#48207228)
Which is, if I understand correctly, the entire point of volatile. Don't use it if you don't want that behavior.

And in fact, in the example given, if there is something (another thread or whatever) that can change the value of x, then you don't know what either number will be. Well, in that circumstance, without volatile, it may print the same number both times, but you still don't know what the number will be (unless the read gets optimized away entirely).

Re:Everything in C is undefined behavior by chuckadams on Wednesday May 20, 2026 @09:42AM (#48207646)
If that behavior is the entire point, then I think the bigger point is that the spec should reflect that and not call it undefined.
Re:Everything in C is undefined behavior by voakbasda on Wednesday May 20, 2026 @10:00AM (#48207954)
I suspect that many undefined behaviors reflect the inability of the standard committee to come to a consensus on the nuances involved. “Punt to the implementers” is a way to allow every tool vendor to select their own expected behavior in those cases.
Re:Everything in C is undefined behavior by chowells on Wednesday May 20, 2026 @11:14AM (#48209173)
You seem to be operating under the assumption "undefined behavior" means "the compiler authors can decide what to do." That's not what it means. It means "any program that causes this behavior to be triggered is not a valid C program, the programmer knows this and did not submit an invalid program, and the programmer explicitly prevented this from happening elsewhere in ways automated analysis cannot detect. Proceed with compilation knowing this branch is impossible."

The spelling for compiler authors getting to choose a behavior is "implementation defined", as the other comment mentions.

Re:Everything in C is undefined behavior by tardedmeme on Thursday May 21, 2026 @03:15AM (#48219015)
It means the C standard does not specify what the program does. Other documents may still specify what the program does. And the program definitely still does something, whether specified or not.
Re:Everything in C is undefined behavior by chowells on Thursday May 21, 2026 @12:45PM (#48225642)
> And the program definitely still does something, whether specified or not.

No. It most definitely does not mean this. Go read the series this is part of: https://blog.llvm.org/2011/05/what-every-c-programmer-should...

It is absolutely critical that people programming in C understand what real compilers in the real world do.

Re:Everything in C is undefined behavior by tardedmeme on Thursday May 21, 2026 @07:07PM (#48229893)
Are you saying the programs in the blog post don't do things? I think they do things. The whole blog post is talking about which things they do.
Re:Everything in C is undefined behavior by MarkusQ on Wednesday May 20, 2026 @10:36AM (#48208586)
Then it should be "implementation defined" rather than "undefined".
Re:Everything in C is undefined behavior by hmry on Wednesday May 20, 2026 @09:11AM (#48207116)
Why is that missing the point? Loading it twice, possibly with different values, is the intended behavior. It's only undefined because the C spec doesn't specify the order of the loads (unlike most other languages which have a perfectly well-defined order for side effects in a single expression).
Re:Everything in C is undefined behavior by rowanG077 on Wednesday May 20, 2026 @10:48AM (#48208791)
What you are describing is implementation defined behavior. Using that is perfectly safe and reasonable. Undefined means this programs is malformed.
Re:Everything in C is undefined behavior by hmry on Wednesday May 20, 2026 @01:24PM (#48211060)
No I'm just repeating what the original comment said, which is that it's explicitly UB:

"5.1.2.4.1 says any volatile access - including just reading it - is a side effect. 6.5.1.2 says that unsequenced side effects on the same scalar object (in this case, x) are UB. 6.5.3.3.8 tells us that the evaluations of function arguments are indeterminately sequenced w.r.t. each other."

If function arguments were sequenced with respect to each other, it wouldn't be a problem.

But actually, maybe the original comment is wrong. Presumably "indeterminately sequenced" and "unsequenced" mean different things, although I don't have a copy of the standard at hand to check.