[Solved] value changes after adding optimization flag in Clang


Attempting to read an uninitialized variable that never had its address taken triggers undefined behavior, which basically means that the program is not required to behave in any particular way.

It’s clear that you were attempting to have the uninitialized variable x in the function x to get the “left over” value of x in tester. With optimization disabled this is how it happens to work. With optimization enabled, the compiler takes advantage of undefined behavior to assume it does not happen and make certain optimizations based on that fact.

So with optimization enabled, the compiler is basically assuming you won’t read an uninialized variable which is why you see different output.

solved value changes after adding optimization flag in Clang