Re:“Fiercely resist any further broadening of the scope of t...
by cwzwarich on Thursday April 14, 2016 @05:30PM
(#11500493)
You can always make the induction variable and loop bound local variables, and the induction variable is almost always a local variable anyways. It was the WebKit coding style for years to use local variables for loop bounds for this reason.
This is the example you're talking about in "What Every C Programmer Should Know":
float *P;
void zero_array() {
int i;
for (i = 0; i < 10000; ++i)
P[i] = 0.0f;
}
If you make the global variable static, then it gets converted to a memset with -fno-strict-aliasing, because the 'P' variable never has its address taken. If you used LTO with sufficient linkage information, the same thing would happen. Similarly, if you pass the array as an argument then the loop gets converted to a bzero call just fine without -fno-strict-aliasing: float *P;
void zero_array_impl(float* a) {
int i;
for (i = 0; i < 10000; ++i)
a[i] = 0.0f;
}
void zero_array() {
zero_array_impl(P);
}
I don't think this is a very good example for these reasons.
There's also the issue of the store changing the array base, as in Chris Lattner's example on "What Every C Programmer Should Know".