Hacker News new | ask | show | jobs
by torstenvl 41 days ago
> it would be better if programmers read deliberate instances of UB as if they literally said "std::unreachable()"

Except that isn't true.

There are basically two kinds of undefined behavior.

The first is undefinable behavior, the kind that easily goes disastrously wrong, like writing to random memory locations.

The second is incidentally undefined behavior, something where there just isn't enough implementation consensus for the standard to settle on it, like realloc(p, 0).

There's no standard terminology I'm aware of to distinguish the two. But the distinction exists.

And where programmers read deliberate instances of UB, it's usually the latter, e.g., writing directly to an address memory mapped to device I/O in that particular environment.

2 comments

> There's no standard terminology I'm aware of to distinguish the two.

There is: implementation-defined behavior. Your example of writing to an MMIO address is such an example, as the standard says casting an int to a pointer is implementation-defined. (However, the C standard has plenty of cases of undefined behavior that probably should have been implementation-defined.)

You seem to be conflating the act of writing to an arbitrary address with the act of assigning that arbitrary address to a pointer to begin with. They are distinct actions.
> There's no standard terminology I'm aware of to distinguish the two. But the distinction exists.

This distinction exists, but not to the compiler. And the compiler is the thing building your binary. You can complain if you like about how UB that could potentially instead have been implementation-defined is UB instead.

But until then, the compiler will mangle your code either way, whether you feel it is fair or not.

Again, that is inarguably and obviously wrong. I cannot understand why you would say things you know to be false.