41 ms·
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 WebK
by cwzwarich 11y ago
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.