6

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?

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271

2 Answers2

9

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".

einpoklum
  • 118,144
  • 57
  • 340
  • 684
aghast
  • 14,785
  • 3
  • 24
  • 56
  • It means that one of two conditions will apply for the lifetime of the pointer, to every object accessed through it or pointers derived from it: (1) the object will not be modified by any means (during the pointer's lifetime), or (2) the object will not be accessed (including reads) by any means other than the pointer or others derived from it. A `restrict`-qualified pointer may identify an object with static or automatic duration if the above conditions hold, but such objects may not be accessed "directly" during the lifetime of the pointer unless they are not modified during that lifetime. – supercat Feb 10 '17 at 17:36
0

Given the function:

int foo(int *restrict p)
{
  *p = 3;
  bar();
  return *p;
}

a compiler can rather easily see--because of the restrict qualifier--that there is no legitimate way by which bar() can access *p. It can thus optimize the above code into:

int foo(int *restrict p)
{
  bar();
  *p = 3;
  return 3;
}

and it can perform such optimization without having to know anything whasoever about bar(). In the absence of the qualifier, the compiler would have to allow for the possibility that the caller might have e.g. passed the address of a global int which is modified by bar(), but because of restrict a compiler wouldn't have to worry about that.

supercat
  • 77,689
  • 9
  • 166
  • 211