HackerTube

CppCon 2016: Chandler Carruth “Garbage In, Garbage Out: Arguing about Undefined Behavior..."

CppConRecommended by 4 HN commenters across 4 months, 2016–2026
YouTube
talk · advanced · evergreen · 2016–2026 on HN

Chandler Carruth's CppCon 2016 talk tries to defuse the heated debate over undefined behavior in C and C++. He explains from the compiler writer's perspective why UB exists and how it enables optimization.

HN points to it whenever undefined behavior comes up, as the best explanation of the reasoning behind UB, though some dispute its framing.

12 comments from Hacker News

@moefh2023-08 · in “A Guide to Undefined Behavior in C and C++”

One man's "broken code produced by the compiler" is another man's "excellently optimized code by the compiler". Where to draw the line is not always clear, but here's a very clear-cut example[1] where emitting a warning would be bad. If you don't want to watch the video, it's basically this: - the code technically contains undefined behavior, but it will never be actually triggered by the program - changing the code to remove undefined behavior forces the compiler to emit terrible code Making the compiler yell at the programmer in this case would be terrible, but it's clearly a consequence of what you're asking. [1] ▶ this video

Recommends View on HN
@Joky2018-11 · in “Trip report: Fall ISO C++ standards meeting”

Actually I discourage my colleague to use unsigned especially because they can overflow. The problem with overflow is that most of the time it is a program bug if there is an overflow that wasn't intended. Using signed arithmetic means that you can actually trap on overflow and catch these bugs (using fuzzing for instance). Chandler explained it nicely in his CPPCon 2016 talk "Garbage In, Garbage Out: Arguing about Undefined Behavior...". I encourage to watch the full talk but here is one relevant example: ▶ this video And here he mentions how Google experimented with this internally: ▶ this video

Recommends View on HN
@lyricaljoke2018-05 · in “Depressing and faintly terrifying days for the C standard [pdf]”

Chandler Carruth's talk on the subject was really useful for my own understanding of the reasoning behind UB: ▶ this video

Recommends View on HN
@kllrnohj2018-03 · in “A Usable C++ Dialect That Is Safe Against Memory Corruption”

The generally recommended thing for catching & fixing this is: link CppCon has also had a lot of great talks on UB and why it is what it is, such as ▶ this video

Recommends View on HN
@atiedebee2026-02 · in “What every compiler writer should know about programmers (Anton Ertl, 2015) [pdf]”

ISO C99 actually defines multiple types of deviating behaviour. What you're describing is closer to implementation-defined behaviour than anything else. The three behaviours relevant in this discussion, from section 3.4: 3.4.1 implementation-defined behavior unspecified behavior where each implementation documents how the choice is made EXAMPLE An example of implementation-defined behavior is the propagation of the high-order bit when a signed integer is shifted right. 3.4.3 undefined behavior behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements Possible 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). An example of undefined behavior is the behavior on integer overflow. 3.4.4 unspecified behavior behavior where this International Standard provides two or more possibilities and imposes no further requirements on which is chosen in any instance An example of unspecified behavior is the order in which the arguments to a function are evaluated. K&R seems to also mention "undefined" and "implementation-defined" behaviour on several occasions. It doesn't specify what is meant by undefined behav