How to tell C or C ++ compiler that pointers are not an alias

I have a function that gets an array of pointers, for example:

void foo(int *ptrs[], int num, int size) { /* The body is an example only */ for (int i = 0; i < size; ++i) { for (int j = 0; j < num-1; ++j) ptrs[num-1][i] += ptrs[j][i]; } } 

What I want to pass to the compiler is that the ptrs[i] pointers are not aliases of each other and that the ptrs[i] arrays do not overlap. How can I do it? My hidden motive is to encourage automatic vectorization.

Also, is there a way to get the same effect as __restrict__ on the std::vector iterator?

+8
c ++ c pointers restrict-qualifier aliasing
source share
3 answers

restrict , unlike the more common const , is a pointer property, not the specified data. Therefore, it belongs to the right side of the declarator modifier * '. [] in a parameter declaration is another way to write * . Combining these things, you can get the desired effect using this function prototype:

 void foo(int *restrict *restrict ptrs, int num, int size) { /* body */ } 

and there is no need for new names. (Not verified). Your mileage may vary. restrict is a pure hint on optimization and really can't do anything constructive with your compiler.)

+9
source share

Something like:

 void foo(int *ptrs[], int num, int size) { /* The body is an example only */ for (int i = 0; i < size; ++i) { for (int j = 0; j < num-1; ++j) { int * restrict a = ptrs[num-1]; int * restrict b = ptrs[j]; a[i] += b[i]; } } 

... should do it, I think, on the C99. I don't think there is any way in C ++, but many C ++ compilers also support the limitation.

+7
source share

In C ++, pointer arguments are not assumed to be aliases if they point to fundamentally different types ("strict anti-aliasing" rules).

In C99, the keyword "constrain" indicates that the pointer argument does not contain any other pointer arguments.

+4
source share

All Articles