
Understanding the C/C++ memory model - ingve
https://davmac.wordpress.com/2018/01/28/understanding-the-c-c-memory-model/
======
rocqua
Wait, data races are undefined behavior?

Is that in the sense that the compiler is allowed to assume they don't happen?
I always figured it was defined behavior but not determinate.

~~~
aorav
The low-level nature of C++ (and also C) makes data races unavoidably
undefined behavior. Imagine a data race that causes a torn write on a pointer.
This pointer could now point pretty much anywhere. The function stack, some
vtable, program data, etc. Any write to that dereferenced pointer will alter
the state of your program in such a way that the behavior of your program will
become undefined. There would be no way for C++ to guarantee any kind of
defined behavior when you can write to arbitrary locations in memory.

~~~
taneq
There are two different things that people can mean when they say "undefined
behaviour" in regards to C/C++. One is what you're talking about, with
"undefined behaviour" meaning "one of several things could happen, you don't
know which, and some of them may be bad."

The other is more specifically talking about code that is deemed "undefined
behaviour" by the spec. There's a surprising amount of this, and the compiler
is explicitly allowed to do anything it wants when encountered, from emitting
'close enough' code to emitting no code at all, to emitting code to order a
pizza to your current location.

If this is the latter case then it could result in random bits of code
mysteriously doing completely unexpected things if the compiler thinks a data
race there is possible. This would make it far harder to track down bugs and
reason about what code might cause such.

------
Annatar
(I screwed up; commented on the story one below instead of one above.) The
story and the comment have been moved here:
[https://news.ycombinator.com/item?id=16250408](https://news.ycombinator.com/item?id=16250408)

~~~
eridius
Your quote doesn't appear in the article, and this article also contains no
code. Did you comment on the correct story?

~~~
m12k
Most likely they intended to comment on this:
[https://news.ycombinator.com/item?id=16249936](https://news.ycombinator.com/item?id=16249936)
(which is next to this item on the front page at the time of writing)

