back

by jeffreyrogers·11y ago·view on hn ↗
> I can't understand what undefined behaviors C has, because it is the most simple and defined language I do know of.

C has lots of undefined behaviors because the language was designed to be easy for compiler writers to implement. As such, a lot of decisions were left to the compiler writers, which is what "undefined" means. Here is a (probably partial) list of undefined behaviors in C. (For a full list you'd have to search through the standard): http://blog.regehr.org/archives/213

Edit: Linked to a C++ article by mistake, so I changed it.

1 comments
Sure, there is a lot of code you could write that results in undefined behaviour. However, none of it is code that you should write, nor is it code that an expert would write.
While your sentence can be true until we get to defining “expert”, the problem is that there are not going to be many experts if to be an expert at C, one has to be able to write useful code that does not accidentally invoke undefined behavior.

- these four bug reports in NTP are for undefined behaviors. You can say that the NTP maintainers are not experts, the problem is that they are the maintainers of NTP: http://bugs.ntp.org/buglist.cgi?emailreporter2=1&emailtype2=...

- a famous undefined behavior was found in OpenSSL recently, but there were and remain plenty more. Sure, the authors of OpenSSL aren't experts, the problem is, they are the people writing and maintaining OpenSSL.

- The Linux kernel has had its share of undefined behaviors. Usually, the developers blame it on the compiler, of which the kernel admits only one (GCC—at least as of recently). There was the time when GCC was blamed for taking advantage of strict aliasing rules, and there was GCC removing of a NULL test on an execution path that dereferenced NULL. If the developers wrote for more compilers, they would realize that the optimizations practiced by GCC are practiced by other compilers too, because they are justified by undefined behavior in the source code.

- I could go on.

I have quoted three useful, widely deployed C pieces of software that have contained undefined behavior, and likely still contain more. Can you name one nontrivial C program, written by an expert according to your definition, that you are confident does not invoke undefined behavior?

Undefined behavior is defined as 'it does what it apparently does without guarantees to future behavior'. So that code that you should write and that an 'expert' would write that implements something that depends in a non-obvious way on undefined behavior (and it is surprisingly easy to do that) will possibly break in hard to detect ways (if it is detected at all, which is more dangerous) at some point in the future when said undefined behavior is changed.

The only way to work around that is not just by becoming an expert at the language but also by becoming an expert at the implementation details of the language and that's a domain that not many programmers are comfortable in. That way you can make sure you stay away from the undefined behaviors as much as possible.

Bad car analogy: not only do you have to learn how to drive the car, you also need to know that the combination of wind across the left front+rain+loud music on the stereo will sometimes cause a wheel to fall off. This is not usually considered a useful level of knowledge and so C programmers (experts too) all find that their knowledge of the limits of C has a partial overlap with the real limits of C as implemented by their particular toolchain. And in between the cracks lots of nasty stuff can happen.

Dead code elimination, different sequences of optimization phases (changing from one compiler to the next) can introduce very subtle bugs in your code and I highly doubt 'most experts' would know how to distinguish between some very innocent code that will almost certainly bork on some standards compliant compiler with optimization 'on' or 'max' versus similar looking code that will work just fine.

C is a beautiful but somewhat tricky mistress.

Then the only 'expert' is a computer, for even the best human will occasionally make undefined behavior inducing mistakes. And 'occasionally' is frequent enough to be a problem.