Search code examples
cstructc99restrict-qualifier

Behaviour of restrict keyword inside structs


The scenario:

Suppose I have a struct type holding a bunch of pointers, all of which declared restrict, and a function which takes a couple of these struct as argument as follows:

struct bunch_of_ptr 
{
    double *restrict ptr00;
    double *restrict ptr01;
    ...
    double *restrict ptr19;
}

void evaluate(struct bunch_of_ptr input, struct bunch_of_ptr output) 
{
    // do some calculation on input and return results into output
}

According to http://www.oracle.com/technetwork/server-storage/solaris10/cc-restrict-139391.html, input.ptrXX and input.ptrYY will be treated as non-aliasing.

The question:

Will the compiler treat input.ptrXX and output.ptrYY as non-aliasing as well?


Solution

  • It should. For every pointer that you declare as restrict somewhere, the compiler can assume that it is the only access to the corresponding data. By declaring these, you actually give the compiler the guarantee.

    If all compilers will take advantage of that information is another question. Passing restrict pointers through struct is not very common, so you'd have to see what your compiler does.