c++ - How to squash bug that only occurs with optimizations enabled -
i'm in bit of pickle. have code until ran fine has started fail when optimizations enabled. code runs fine optimizations disabled -o0
both -o1
, -o2
cause issue occur (i don't use -o3
).
unfortunately, can't post source code (it's rather involved , proprietary) i'm going attempt minimal working example. meanwhile i'd love hear experience manner of c++
capable of resulting in unexpected optimizations enabled (what gotchas? should review first?).
here's @ least give general idea of code first found issue:
struct slice { const char* m_data; size_t m_size; } char scratch[1024]; slice result; read(read, &result, scratch); // optimizations disabled, result contains 40 bytes of data optimizations enabled m_size zero!?
this nothing fancy, simple , straightforward c++
.
i'm using gcc-4.8.1
have same issue clang++
on os x (visual c++ on windows not appear have issue).
usually such unstable behaviour (different results different optimization options, or different results under different compilers, or different results on different runs) happen due undefined behavior, , due memory management problems (uninitialized value use, bad pointer management, etc.)
there special tools identify (though not all) such problems.
one of them valgrind, build program , call valgrind ./program
. track many of memory access problems, uninitialized values, etc. though decreases performance substantially.
another address sanitizing option of compilers. gcc, build program -fsanitize=address
, , run program normally. other compilers there may similar options. catches bit different set of problems valgrind, , not have perfomance problems.
in both cases, can build program optimization option. problem show anyway. false positives may show up, might depend on code.
another set of tools static code analyzers such cppcheck.
of course, usual, can try debugging program or adding debug output etc, might not effective, because change program can make behavior normal.
Comments
Post a Comment