4 ms·
By definition the undefined memory behavior is only in the undefined parts of the spec modulo bugs. The spec is written against an abstract virtual machine and
by vlovich123 2y ago
By definition the undefined memory behavior is only in the undefined parts of the spec modulo bugs. The spec is written against an abstract virtual machine and C was one of the first to pioneer such a concept and why it was so successful at getting ported everywhere.
- dangitman 2y ago[dead]
- steveklabnik 2y ago> I was under the impression most of how c interacts with memory is part of the undefined part of the spec For a long time, the memory model, formally speaking, was underspecified. Both C and C++ agreed on and added a memory model in C11 and C++11. > fencing You can add a fence via this API: https://en.cppreference.com/w/c/atomic/atomic_thread_fence https://en.cppreference.com/w/c/atomic/atomic_thread_fence > This varies per arch Right, so what assembly this API will emit depends on the underlying architecture details. Notably, the Linux kernel does not use the standard C memory model, it uses its own.
- dangitman 2y ago[dead]
- vlovich123 2y agoTo be clear, that’s the definition of the memory model under concurrent execution. I’m pretty sure the single threaded version was well defined
- steveklabnik 2y agoMy understanding is that “memory model” is definitionaly about what happens when you add parallelism and/or concurrency. Regardless, this isn’t a slight against C. Most languages don’t have an explicit memory model at all.
- vlovich123 2y agoI introduced the word memory model so that’s on me for that error - definitionally memory model does refer to concurrency. However Op used “memory behavior”. The C standard definitely has defined and defined and undefined memory behaviors even pre C11. For example, dereferencing null is an undefined single threaded memory behavior that bit the kernel when the compilers started optimizing from assumptions about never reaching UB code. But for example there’s lots of defined behaviors like what happens when you dereference an aliased pointer. And the Linux kernel definitely defined a memory model for itself before C and made all architectures conform to that.
- steveklabnik 2y agoYeah that's fair. As I said, I'm not trying to imply something was bad here before C11. I thought "underspecified" instead of "unspecified" would communicate "some of it was defined but some of it wasn't but it's been fully so for a while now" but maybe that missed the mark.