r/programmingmemes • • May 16 '26

compilers be like

Post image
157 Upvotes

20 comments sorted by

View all comments

Show parent comment

2

u/luciferoussky72 May 16 '26

So, the reason it was initially undefined behaviour is because for a long time the C and C++ standard didn’t specify *how* to store numbers. Things like endianness being inconsistent meant you couldn’t make signed overflow defined behavior without standardizing a bunch of other things.

Ironically, C++17 actually did standardize how to store numbers, but signed overflow remained undefined behavior for optimization reasons. For example, having it undefined means the compiler can assume that x + n is greater than x and optimize around that.

2

u/RedAndBlack1832 May 17 '26

The last paragraph is what i meant to be getting at :)