3 ms·
I've never really understood why this couldn't be slotted in as "implementation defined behavior". `add` has a well defined meaning on every platform just becau
by bbatha 9y ago
I've never really understood why this couldn't be slotted in as "implementation defined behavior". `add` has a well defined meaning on every platform just because it differs shouldn't give the compiler license to change the meaning of my program for that platform. It should however platform specific optimizations based on overflow, etc.
Could somebody provide me with an example of truly undefinable behavior? (Perhaps data races?)
- jcranmer 9y agoWhen C was first written, it wasn't necessarily possible on some architectures to get traps to trigger at the exact instruction on some hardware. Thus, any potentially-trapping instruction had to be undefined instead of implementation-defined.
- tom_mellior 9y ago> truly undefinable behavior Memory corruption is pretty undefinable. Imagine writing data through a wild pointer. The program might segfault immediately, or compute a wrong result but not crash, or crash at an arbitrary later point, or delete all your files, or... It makes no sense to try to enumerate all that might happen, so this cannot really be implementation defined (where the implementation must document what will happen).
- adrianratnapala 9y agoRight, but it is not the plus operation that does this. You can reserve undefined behaviour to things like "write to a bad pointer". Then a particular ABI could say "if your pointer overflows (by a our system specific meaning of overlow) then you have a bad pointer". Merely doing a pointer increment just gets you implementation-defined behaviour. The UB only happens if you plough on. Such a setup wouldn't do much to save us from pointer bugs. But it would make optimizers more predictable.