I've noticed a heavy use of the restrict
keyword in one of our legacy projects.
I understand the rationale for restrict
, but I question its useful-ness when applied to some of these functions.
Take the following two examples:
void funcA(int *restrict i){
// ...
}
void funcB(int *restrict i, float *restrict f){
// ...
}
int main(){
int i = 1;
float f = 3.14;
funcA(&i);
funcB(&i,&f);
}
Is there any valid reason one might tag the parameters of funcA
and funcB
with restrict
?
funcA
only takes 1 parameter. How could it have the same address as anything else?
funcB
takes parameters of different types. If they were the same address, wouldn't that already be breaking the strict aliasing rule?
restrict
is not meaningless with single-pointer-parameter functions.The restrict
keyword is a declaration of intent, for improved optimization. It means that the objects pointed to by the given pointers will not be pointed to by anything else for the life of the (in this case) function parameters.
You don't show the code of the functions, so there may be static variables held inside. Restrict is a guarantee that those static variable don't alias the parameters.
There may be global variables not shown in your example. Restrict is a guarantee that those global variables don't alias the parameters.
In reality, you're right: it's likely that someone just went a little crazy with restrict. But restrict does not mean "this parameter and that parameter". It means "this pointer and any other pointer".