Skip to content

Instantly share code, notes, and snippets.

@Keith-S-Thompson
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Keith-S-Thompson/be3827ee243d4367f9bf to your computer and use it in GitHub Desktop.
Save Keith-S-Thompson/be3827ee243d4367f9bf to your computer and use it in GitHub Desktop.
g++ bug demo
// http://stackoverflow.com/q/26067473/827263
// Using g++ 4.8.2 or 4.9.1, prints "ok" with "-O0" or "-O1", "BUG!" with "_O2" or "-O3".
// Apparently the problem also occurs with Microsoft Visual C++ 11.00.60610.1.
// clang++ 3.5.1 does not exhibit the problem; it prints "ok" at all optimization levels.
#include <iostream>
static int count = 0;
static bool bug = false;
void bar() {
extern int upper_bound;
upper_bound--;
count ++;
}
void foo()
{
extern int upper_bound;
for (int i = 0; i < upper_bound; ) {
bar();
count ++;
if (count > 20) {
bug = true;
return;
}
}
}
int main() {
foo();
if (bug) {
std::cout << "BUG!\n";
}
else {
std::cout << "ok\n";
}
}
int upper_bound = 10;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment