3 ms·
Yes, this is an example of UB leaving memory in a bad state. If you want an example of something that is not UB leaving memory in a bad state, here is some Go
by tsimionescu 2y ago
Yes, this is an example of UB leaving memory in a bad state.
If you want an example of something that is not UB leaving memory in a bad state, here is some Go code:
global := 7;
func main () {
go func() {
global = 1000000;
}()
go func() {
global = 10
}()
fmt.Printf("gloabl is now %d")
}
The two concurrent writes may partially overlap, and global may have a value that is neither 7 nor 10 nor 1000000. The program's memory is in a bad state, but none of this is UB in the C or C++ sense. In particular, the Go compiler is not free to compile this program into something entirely different.
Edit: I should also note that a similar C program using pthreads or win32 threading for concurrent access is also an example of a program which will go into a bad state, but that is not UB per the C standard (since the C standard has no notion of multithreading).
- saagarjha 2y agoThe C standard definitely has opinions on races
- tsimionescu 2y agoPlease share a link.
- saagarjha 2y agoI mean like C11 has a whole atomics addition and memory model to go with it