c / intermediate
Snippet
The restrict Pointer Qualifier
The 'restrict' keyword is a hint to the compiler that the pointer is the only way to access the underlying data. This prevents aliasing and allows for aggressive optimizations like vectorization.
snippet.c
1
2
3
4
5
void vector_add(int * restrict a, int * restrict b, int * restrict res, int n) {for (int i = 0; i < n; i++) {res[i] = a[i] + b[i];}}
Breakdown
1
int * restrict a
Informs the compiler that 'a' does not overlap with 'b' or 'res'.
2
res[i] = a[i] + b[i];
Since no aliasing occurs, the compiler can safely reorder or vectorize these operations.