I'm in C and I have an int ** (2 dimensional array, it's basically a matrix, it's actually set up a little bit complicated but there's no internal aliasing, so it's not relevant).

is this int** restrict or int * restrict * restrict my instinct is the former since restrict only really applies to variables but the compiler is merrily happy to accept both (although free gets upset at discarding the restrict).

For reference the only way the code access i[4][5] is by using i[4][5] . I.e. there are NO other variables such that say j=&(i[4][5]) and the internal array is structured such that say i[a][b] and i[c][d] ALWAYS point to different integers unless a==c && b==d is true.

I need this to vectorize and my compiler (clang) is being...twitchy? it works mostly but I have to do a bunch of work to insure it can't see any other int * anywhere when the compiler is working on the array (basically I move the array processing to a function, process the array, then collect the results). The int *'s I'm dealing with are...unrelated to the array (they are MPI out values), so this is kind of annoying that the compiler thinks there might be a way one of them might alias an internal block

3 Replies 3

The restrict in int * restrict *P says something about the pointers “in” P, that is, the various objects designated by P[i] for various i. For each of those pointers, it says that, if that pointer is used to access an object (some P[i][j]), then either using P[i] is the only method that will be used to access that object or that object’s value does not change (during execution of the block in which the declaration applies). (Note that using a pointer to access an object includes indirectly using it, such as int *Q = P[i]; printf("%d\n", Q[j]);.)

The restrict in int * * restrict P says something about P. It says that, if P is used to access an object (some P[i]), then either using P is the only method that will be used to access that object or that object’s value does not change. This restrict says nothing about accessing the second-level objects, P[i][j].

int * restrict * restrict P says both of those things.

is this int** restrict or int * restrict * restrict

It is not inherently either. restrict-qualification expresses an assertion about how a particular piece of code will use the value of a pointer, and what assumptions the compiler can make about that use. You probably should not use restrict qualification other than for function parameters. If the compiler can determine by analyzing the code that there is no possibility of aliasing, then restrict does you no good whatever.

If you are passing this thing as a function argument, then

  • int * restrict * says, roughly, that no element the function accesses via one of the row pointers will also be accessed via any other pointer during any given execution of the function. On the other hand,

  • int ** restrict says, roughly, that none of the row pointer values accessed via the argument will also be accessed via any other pointer during any given run of the function.

  • int * restrict * restrict makes both assertions.

Note, again, that this is an assertion to the compiler. For callers of a function with one or more restrict-qualified parameters, on the other hand, it is a constraint.

Overall, using restrict qualification in code you write is almost always a premature optimization, if it is any kind of optimization at all. If you have to ask the question then you probably shouldn't be using restrict, and if you were in a position to answer the question then you probably wouldn't use restrict.

Please post this as a normal Q&A question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.