C and C++ Prioritize Performance over Correctness (2023)

https://news.ycombinator.com/rss Hits: 5
Summary

C and C++ Prioritize Performance over Correctness Posted on Friday, August 18, 2023. PDF The original ANSI C standard, C89, introduced the concept of “undefined behavior,” which was used both to describe the effect of outright bugs like accessing memory in a freed object and also to capture the fact that existing implementations differed about handling certain aspects of the language, including use of uninitialized values, signed integer overflow, and null pointer handling. The C89 spec defined undefined behavior (in section 1.6) as: Undefined behavior—behavior, upon use of a nonportable or erroneous program construct, of erroneous data, or of indeterminately-valued objects, for which the Standard imposes no requirements. Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message). Lumping both non-portable and buggy code into the same category was a mistake. As time has gone on, the way compilers treat undefined behavior has led to more and more unexpectedly broken programs, to the point where it is becoming difficult to tell whether any program will compile to the meaning in the original source. This post looks at a few examples and then tries to make some general observations. In particular, today’s C and C++ prioritize performance to the clear detriment of correctness. Uninitialized variables C and C++ do not require variables to be initialized on declaration (explicitly or implicitly) like Go and Java. Reading from an uninitialized variable is undefined behavior. In a blog post, Chris Lattner (creator of LLVM and Clang) explains the rationale: Use of an uninitialized variable: This is commonly known as source of problems in C programs and there are many tools to catch th...

First seen: 2025-03-31 16:42

Last seen: 2025-03-31 20:43