Reputation: 69
For a function like
int test(void) {
static int x = 0;
x++;
return 0;
}
is a C compiler allowed to optimize out x
?
For reference, neither GCC 6.3.0 or Clang 3.9 optimize out incrementing x
with -O3.
Upvotes: 5
Views: 415
Reputation: 272517
The compiler would be within its rights to optimise away this function completely, given it has no observable side effects (from the point of view of the C standard).
As to why your compilers aren't doing so, I can't explain that! (Though of course, they're under no obligation to do so.)
Upvotes: 2