3 ms·
There was a post here not too long ago about how C/C++ compiler writers have consistently taken the wiggle room of "undefined behavior" to do terrible, terrible
by cratermoon 5y ago
There was a post here not too long ago about how C/C++ compiler writers have consistently taken the wiggle room of "undefined behavior" to do terrible, terrible, damage to otherwise fine code. https://news.ycombinator.com/item?id=27221552 https://news.ycombinator.com/item?id=27221552
- saagarjha 5y agoIn this case there is no undefined behavior, the order is just unspecified.
- cratermoon 5y ago> unspecified how is that different from undefined?
- saagarjha 5y agoThe existence of undefined behavior indicates that the program is invalid. Unspecified behavior is just something that is not precisely defined.
- Kranar 5y agoUndefined behavior is unbounded in both space and time in terms of program semantics, that is, a program that will engender undefined behavior may do so at any point during the course of program execution (unbounded in time) as well as may be observed within any memory region (unbounded in space). A colloquial way of saying this is that a compiler is free to do anything it wants anytime it wants in the presence of undefined behavior. Unspecified behavior is bounded both in space and time. Unspecified behavior may only affect program semantics within the statement in which it is engendered and may only be observed within the region of memory that is written to or read from. It is often said that undefined behavior is invalid, and one can sympathize with that statement as it's commonly repeated, but it's not true. All the standard says about undefined behavior is that the standard imposes no requirements about program behavior, it does not state that the program is invalid or that the program must behave in some kind of invalid manner. Almost all non-trivial C++ programs engender undefined behavior in a manner documented by the compiler and do so in a safe and principled way.
- mhh__ 5y agoIn English, that's a reasonable point but within the C++ standard behaviour is declared to be specifically undefined in the text - that is to say, we are telling you, you may write this construct however said construct is not C++
- rocqua 5y agoNote that this post was heavily discussed and disputed. The question "is undefined behavior treated correctly?" is rather contentious. It certainly is the case that most compiler writers have decided this is okay. And I personally tend to agree with them.