Sujet : Re: Loops (was Re: do { quit; } else { })
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.cDate : 11. May 2025, 11:02:25
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vvpsji$4jht$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
User-Agent : Mozilla Thunderbird
On 11/05/2025 10:21,
Muttley@dastardlyhq.com wrote:
On Sat, 10 May 2025 14:29:50 -0700
Tim Rentsch <tr.17687@z991.linuxsc.com> gabbled:
Muttley@dastardlyhq.com writes:
>
On Sat, 10 May 2025 06:43:38 -0700
Tim Rentsch <tr.17687@z991.linuxsc.com> gabbled:
>
never necessary). Also it isn't easy to think of a good substitute
word that might be given for this use of 'static', so maybe the
>
Isn't it?
>
Where "static" means local to a module the words
"local","module","limit" spring to mind which are far closer to
the intended meaning. Reusing "static" seems somewhat perverse
IMO.
>
The use I'm talking about here may be illustrated as follows:
>
double
average( double values[ static 10 ] ){
double total = 0;
for( int i = 0; i < 10; i++ ){
total += values[i];
}
return total / 10;
}
>
What word would you suggest to be used in place of 'static'
there?
If I knew what the hell it was supposed to do I'd tell you.
Using "static" inside array parameters is, IME, extremely rare. It was added in C99, and tells the compiler that whenever "average" is called, the "values" parameter points to an array of at least 10 doubles. It does not affect the signature of the function or compatibility with any other declarations, and is AFAIK rarely checked by compilers.
In an example like the one above, it is completely useless for compilation - it tells the compiler nothing that it does not already know. An optimising compiler will see that you are accessing values[0] to values[9], and if it can get better results through vectorising, prefetching, etc., then it will do so. (You can argue that the "static 10" is still useful as an indicator to human readers, but I am not convinced of that.)
However, there are possible situations where it could be useful to the compiler. Consider instead this function :
double average(double values[static 4]) {
double total = 0.0;
for (int i = 0; i < 3; i++) {
total += values[i];
}
return total / 3;
}
Without the "static 4", the compiler can only assume that it can access up to 3 doubles from the "value" array. But with "static 4", it knows it is safe to read "values[3]" even though the code never needs to do so. And that means it can use a "prefetch 32 bytes" instruction, or a "load 4 doubles into a SIMD register" instruction, if these can result in faster code.
When "[static X]" was added to C99, it was thought that this could be a significant help in some code. It certainly /can/ be helpful for some code (consider a case where the loop range is from another parameter rather than a fixed value like the examples above), but the combination of super-scaler and re-ordering modern processors and smarter modern compilers means it is very rare that it is helpful. And in cases where it could increase efficiency and you really need maximal efficiency, you are almost certainly better off using compiler-specific features (like gcc's __builtin_prefetch).
So the obvious answer to Tim's question of what to use instead of "static" is "nothing at all" :-)