restrict in C
restrict promises the pointers do not overlap, which lets the compiler optimize.
void add_arrays(int *restrict out, const int *restrict a,
const int *restrict b, size_t n) {
for (size_t i = 0; i < n; i++) {
out[i] = a[i] + b[i];
}
}
void scale_in_place(int *values, size_t n, int factor) {
for (size_t i = 0; i < n; i++) {
values[i] *= factor;
}
}
How it works
- Two restrict pointers may not address the same object.
- Breaking the promise is undefined behavior, not a warning.
- memcpy has it; memmove deliberately does not.
Keywords and builtins used here
add_arraysconstforintrestrictscale_in_placesize_tvoid
The run, in numbers
- Lines
- 12
- Characters to type
- 267
- Tokens
- 97
- Three-star pace
- 105 tpm
At the three-star pace of 105 tokens a minute, this run takes about 55 seconds.
Step 2 of 2 in Qualifiers, step 7 of 13 in Unions, bitfields & undefined behavior.