When the internet finds some premium nasal demons
25 comments
@beeoproblem @aeva @mdiluz The reasoning I've seen is: @RAOF @beeoproblem @mdiluz if you're missing a main function other functions don't get promoted to main. I tested it out in godbolt earlier, what happens is the ret instruction in main gets optimized out, thus the program counter just advances to the next instruction in memory which is the other function. The entry points for main and the other function still exist. @RAOF @beeoproblem @mdiluz gcc and msvc both do what you'd expect. Clang and zig's c++ compiler both optimize away the loop and the return. @beeoproblem @aeva @mdiluz the return call from main() got optimised away and so the CPU doesn’t jump away, and keeps taking further instructions from the assembly, which are the body of unreachable() @AgreeableLandscape @aeva @mdiluz because of linking, the compiler doesn't know if the object will be linked. The function can be called from another object. @ekg @AgreeableLandscape @mdiluz this exactly. To Agreeable Landscape's concerns, if you mark "unreachable" as static (indicating it only exists for the translation unit), then clang removes it. It's not a big spooky zero day, this is just how linking works 🤷♀️ @mdiluz clang 15 on x86-64 with -O1: @thor @mdiluz damn, and I remember when it would take a Cray a good half a minute to complete an infinite loop. @mdiluz There's a reason I don't like C++ anymore. Too much undefined behavior. And it doesn't just do a logic bug, it can do anything... |
Credit: https://www.reddit.com/r/ProgrammerHumor/comments/10wur63/isnt_c_fun/