typestar

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

  1. Two restrict pointers may not address the same object.
  2. Breaking the promise is undefined behavior, not a warning.
  3. memcpy has it; memmove deliberately does not.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 2 in Qualifiers, step 7 of 13 in Unions, bitfields & undefined behavior.

← Previous Next →