6 ms·
Start by removing gratuitous undefined behavior from the dialect: undefined behavior which has not reason to exist because it arises from of the lack of require
by kazinator 11y ago
Start by removing gratuitous undefined behavior from the dialect: undefined behavior which has not reason to exist because it arises from of the lack of requirements with regard to some translation aspect of the language, and not anything related to a run-time difficulty.
For instance, the unspecified order in which function arguments are evaluated, or the operands of most operators, or initializers in a declarator and such. In combination with certain uses of side effects, this creates undefined behavior (and even when not undefined, it can create surprises and bugs). This is purely a C semantics problem, not connected to the way the machine works, or the large scale way in which programs and their data fit together.
And let us note that even if evaluation order is pinned down by the language semantics, the compiler can still re-arrange it anyway, when it can recognize opportunities in which re-ordering evaluation makes no difference (the result, including all effects, are still as if it had been left to right).
Contrast that with, say, bounds checking arrays, where the neglect to do so is justified by the fact that pointers don't carry the size information, since they are mapped to machine addresses, which is a translation decision that has deep, non-localized semantic implications. Wider pointers don't fit into a register, which affects how they are passed between functions. They take up more space in every data structure which has pointers, and so on. There is a tangible, external difference, and performance impact.
A reasonable dialect in C should sacrifice safety only when there is a tangible performance issue.
Undefined behaviors at translation time (particularly in the preprocessor!) should be completely banished.
Here is one: there is no reason why a preprocessor token pasting operation (A ## B) which results in an invalid token should be undefined behavior. The execution of the preprocessor is quasi cost-free. Whether or not two elements pasted together to form a valid token can be checked, and diagnosed. The few preprocessor cycles which that requires are worth it!
The fact that it's not practical to banish or diagnose all undefined behaviors (nor desirable or reasonable to do so, since UB is an area for useful extensions) shouldn't be used as an excuse not to eliminate some of the silly ones.
- kevin_thibedeau 11y agoThe undefined behavior is there so that the language can be used across heterogeneous systems. 9-bit chars, 36-bit words, 1's complement arithmetic, decimal registers, NULL somewhere other than address 0? You can have all that with standard compliant C.
- marvy 11y agoFine, so keep those aspects. There still remains plenty of relatively pointless undefined behavior. For instance, is the following well defined? int f(int* p, int* q){return ++p + ++q;} It depends! It depends on whether p == q. If so, it is not defined, and on high optimization levels, compilers will tend to give the wrong answer, but on low levels not so much. I bet a lot of people (but probably not all) would be willing to take the speed hit so that this becomes well defined, as it is in most other languages. (Note: I haven't fact checked anything I wrote here; no doubt some language lawyer will correct me if I'm wrong.)
- kazinator 11y agoWe don't even have to take the speed hit. Why? Because here is how the unspecified evaluation order benefits the above scenario: the compiler doesn't have to reason about whether p == q. But since C99 we have had a tool by which we can tell the compiler, "trust me, p != q". Namely: int f(int * restrict p, int * restrict q) { ... } So with this, even if we have strict left to right order, it can still reorder the code the same as before.
- scott_s 11y agoOf your examples, I believe only dereferencing 0 is undefined behavior. The rest are, I believe, implementation defined. It is implementation defined behavior which allows C to be used across heterogenous systems. I talk about the difference elsewhere in this thread: https://news.ycombinator.com/item?id=10839389 https://news.ycombinator.com/item?id=10839389
- marvy 11y agoI agree with you about the preprocessor example, but I bet that this is just a case of the standard being silly and real implementations are doing the right thing (that is, you will get an error message). I also agree with you about bounds checking arrays: this is the wrong problem to start from when there is so much lower hanging fruit. I want to agree with you about unspecified evaluation order, but I worry about possible side effects. If tomorrow someone waved a magic wand and all C compilers have a command line switch --left2rite that forces left-to-right evaluation order, how many programs that currently "work" by accident will suddenly break if this switch is enabled? Maybe it's very few, maybe so few that the change is a net benefit. But are you sure? The programs most likely to break are not the ones with the best test suites. I will say this though: implementing this switch is a good idea anyway, for at least two reasons. New programs can use it (at least if they don't worry about portability to a compiler that doesn't support it). More importantly, we can then start doing experiments to gather data and try to answer the question of how widespread the problem is. Until then, we'll never know.