`restrict` — C Keyword
`restrict` — C Keyword
The restrict keyword in C99: asserts that a pointer is the only alias to its target, enabling better optimization.
`restrict` — C Keyword
The restrict keyword in C99: asserts that a pointer is the only alias to its target, enabling better optimization.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
restrict (C)A type qualifier for pointers. Asserts that the pointer is the sole access path to the object it points to (no aliasing). Allows the compiler to produce more efficient code.
Type* restrict ptr;
return-type func(Type* restrict a, Type* restrict b);
#include <stdio.h>
/* Without restrict, the compiler must assume a and out might alias */
void add_arrays(const float* restrict a,
const float* restrict b,
float* restrict out,
int n) {
for (int i = 0; i < n; ++i) {
out[i] = a[i] + b[i]; /* compiler may now vectorize freely */
}
}
int main(void) {
float a[] = {1.0f, 2.0f, 3.0f};
float b[] = {4.0f, 5.0f, 6.0f};
float out[3];
add_arrays(a, b, out, 3);
for (int i = 0; i < 3; ++i) {
printf("%.0f ", out[i]); /* 5 7 9 */
}
printf("\n");
return 0;
}
restrict contract (passing aliasing pointers) is undefined behavior.restrict extensively (e.g., memcpy, strcpy, printf).restrict does not exist in C++; it is a C99 feature (some compilers offer __restrict as an extension).restrictint main() {
// Pick one facility from this reference page.
// Write the smallest program that exercises its main precondition,
// complexity rule, or lifetime constraint before scaling up.
return 0;
}