Liste des Groupes | Revenir à l c |
On Sun, 11 May 2025 12:02:25 +0200No, C (and the C compiler) /does/ care if it something is safe or not. But it believes the programmer if the programmer says it is safe. Thus if the programmer writes code that accesses "values[3]", the compiler may assume it is safe to do so at other times too. But if the programmer never accesses beyond "values[2]", the compiler cannot assume it is safe to access "values[3]". If you want the compiler to be able to generate code that is more efficient by accessing "values[4]", you have to tell it that it is safe to do so. And using "static 4" here in the parameter is one way of saying that.
David Brown <david.brown@hesbynett.no> gabbled:double average(double values[static 4]) {Not sure I follow. C doesn't care if its safe or not, it'll just try and
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
read them anyway and if it can't and there's a memory leak or crash, well, tough luck mate. So I don't see why it would make a difference to the resulting
assembler.
Les messages affichés proviennent d'usenet.