So a lot of people have posted on stack overflow about how compiler optimizations introduce bugs into their program. However, I'm having the opposite problem. I'm writing a C++ program and I have a bug that seems like undefined behavior that only shows up when I set XCode's compiler to off (None[-O0]). If I set it to "fastest, aggressive optimizations [-Ofast]", then the bug disappears.
So I was wondering if anyone has any experience with optimization getting rid of a bug (instead of the more common case of optimization introducing bugs). What are some common causes of this? I'm kind of at a loss of what to do or where to check.
Optimization can change evaluation order of expressions, so if your bug is the result, for example, of a specified evaluation order of foo()
and bar()
in:
some_function(foo(), bar());
then different optimization settings will expose the bug while other ones will hide it.
There are lots of other possibilities.
Enabling optimizations can cause freed stack/heap memory to contain valid data for a longer period of time than in a debug build. Some compilers even scrub freed stack/heap memory in debug builds, meaning that freed data is immediately invalid.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.