Liste des Groupes | Revenir à cl c |
On 5/11/25 06:02, David Brown wrote:The wording of the C standard (C11, as that's what I have open at the moment) is :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:More precisely, it makes it undefined behavior for values to point to an>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
array of less than 10 doubles.
Indeed. I am also a big fan, in general, of making mistakes in code into compiler errors when possible - I'd rather the compiler found my bugs than leave it to run-time testing!does not affect the signature of the function or compatibility with anyI'd have preferred it if violating that requirement was a constraint
other declarations, and is AFAIK rarely checked by compilers.
violation, but it can't be, because there are many cases where a
compiler cannot be sure how long the array is that a pointer points at.
However, the fact that the behavior is undefined justifies a compilerUnfortunately, that is not quite true. A C programmer is free to write whatever nonsense and run-time UB they like, as long as that UB is not actually "executed". So a compiler, if it aims to be conforming and to accept code with well-defined behaviour, has to be sure that the bad code in question would inevitably be executed before it is justified in rejecting the code with an error.
reacting to the case when it can be sure that the requirement will be
violated.
That's the main reason I like this feature, and dislikeThe C99 rational says it was added for efficiency reasons. But often efficiency and static error analysis go together - the more information the compiler has, the better it is at both tasks. And I agree with your attitude of emphasising the static error analysis as being more important than the efficiency considerations in most cases.
compilers that fail to take advantage of that opportunity. I never
considered it to be about efficiency, though there are cases where it
can result in more efficient code.
That is the case for its potential usefulness in static error checking, but not the case for its potential usefulness in optimisation.In an example like the one above, it is completely useless forIt's main potential usefulness is not in the definition of the function,
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.)
but in calls to the function. If the calls occur in a different
translation unit from the definition, the compiler does not have the
needed information.
Les messages affichés proviennent d'usenet.