CppCon 2016: Michael Spencer “My Little Optimizer: Undefined Behavior is Magic"
Why Stack Overflow recommends it
Stack Overflow users link it when explaining why undefined behavior can produce surprising results under optimization, such as signed overflow, realloc, null pointers and uninitialized variables.
Recommended by 6 people across 6 different months, 2016 to 2023.
What it is
A CppCon 2016 talk by Michael Spencer, a compiler engineer at Sony PlayStation, on how and why modern optimizers exploit undefined behavior in C and C++ programs. It walks through concrete examples, including a realloc() case, of how compilers reason from the assumption that UB never happens.
What people said
Strong rec alter_igel · 2021-09 · Stack Overflow · on “Why does -O3 in gcc seem to initialize my local variable to 0, while -O0 does not?”
I recommend the talk Michael Spencer “My Little Optimizer: Undefined Behavior is Magic" [this video] . It's a bit detailed and specific to one compiler, but it gives definitions for what Undefined Behaviour is and means, and shows very concretely the strange things that compilers do to your code when UB is involved.
Recommends Matthieu M. · 2016-10 · Stack Overflow · on “At what point in the loop does integer overflow become undefined behavior?”
First, let me correct the title of this question: Undefined Behavior is not (specifically) of the realm of execution. Undefined Behavior affects all steps: compiling, linking, loading and executing. Some examples to cement this, bear in mind that no section is exhaustive: the compiler can assume that portions of code that contain Undefined Behavior are never executed, and thus assume the execution paths that would lead to them are dead code. See What every C programmer should know about undefined behavior [LINK] by none other than Chris Lattner. the linker can assume that in the presence of multiple definitions of a weak symbol (recognized by name), all definitions are identical thanks to th
Recommends TartanLlama · 2016-10 · Stack Overflow · on “At what point in the loop does integer overflow become undefined behavior?”
You might enjoy My Little Optimizer: Undefined Behaviour is Magic [this video] from CppCon this year. It's all about what optimizations compilers can carry out based on undefined behaviour.
Recommends Michael Kenzel · 2018-07 · Stack Overflow · on “Why does 'undefined behaviour' exist?”
Undefined behavior exists mainly to give the compiler freedom to optimize. One thing it allows the compiler to do, for example, is to operate under the assumption that certain things can't happen (without having to first prove that they can't happen, which would often be very difficult or impossible). By allowing it to assume that certain things can't happen, the compiler can then eliminate/does not have to generate code that would otherwise be needed to account for certain possibilities. Good talk on the topic [this video]
Recommends Stephen Newell · 2022-11 · Stack Overflow · on “std::memset with zero count and invalid pointer”
No, and this has been covered multiple times at Cppcon (don't have the bandwidth right now, but I think [LINK] or [this video] will cover it). You're not allowed to pass NULL as either the source or destination, and compilers are free to optimize accordingly. See also: [LINK]
Recommends Jeremy Friesner · 2023-04 · Stack Overflow · on “Is a reserved vector thread-safe?”
@WayneTseng the programmer might not care, but the compiler's optimizer might still care -- in particular, the compiler assumes no undefined behavior exists when it does its optimizations, which means that if undefined behavior actually does exist, life can get extremely weird and non-intuitive -- and not just "in theory" but in real life... you may spend a lot of time chasing ghosts :) [this video]
Recommends Jeremy Friesner · 2023-09 · Stack Overflow · on “What happens after you delete a ptr C++”
If you haven't seen it, I recommend watching Michael Spencer's talk on all the unexpected ways undefined behavior can bite you -- modern C++ compilers are quite a bit more nefarious than many people might imagine :) [this video]
Cites Alexey Frunze · 2016-10 · Stack Overflow · on “realloc(), lifetime and UB”
There's a recent CppCon2016 talk My Little Optimizer: Undefined Behavior is Magic [this video] , which shows the following code (26 mins into the talk). I beautified it a bit: #include <stdio.h> #include <stdlib.h> int main(void) { int* p = malloc(sizeof(int)); int* q = realloc(p, sizeof(int)); *p = 1; *q = 2; if (p == q) { printf("%d %d\n", *p, *q); } return 0; } The code has undefined behavior (p becomes invalid after realloc() even if realloc() returns the same pointer) and when compiled may print not only "2 2", but also "1 2". What about a slightly modified version of the code?: #include <stdio.h> #include <stdlib.h> #include <stdint.h> int main(void) { int* p = malloc(sizeof(int)); uintptr_
Cites Schwern · 2016-10 · Stack Overflow · on “realloc(), lifetime and UB”
@AlexeyFrunze Do I see the point... not sure? Why the language permits it... the speaker made the point in the beginning of their talk [this video] , undefined means "no requirements at all". LLVM chose to interpret that as assuming undefined behavior cannot happen and optimizes accordingly, another compiler might act differently or not optimize as aggressively. That's undefined behavior for you. If you're asking why the C/C++ standards allow so much undefined behavior, I can't answer that.
Cites Stephen Newell · 2018-03 · Stack Overflow · on “Signed Integer value overflow in C++?”
@Still-InBeta - I don't have the bandwidth right now to go through all the old Cppcon talks (although it sounds fun). If memory serves me correctly, this is the correct talk: [this video] (could also be this one though: [LINK] ). If somebody wants to do the leg work and verify/correct the link, I'm happy to edit the answer.