On this page
  1. restrict
  2. Availability
  3. Syntax & Overview
  4. Notes & Pitfalls
  5. Example

restrict

Prev: inline | Up: builtins | Next: volatile
Rel: Memory Layout & ABI
Ref: cppreference — restrict
Tags: #public #prog #c #builtins #tier/specialized


Availability

Built-in (Type Qualifier (C99)) — requires no #include header.

Syntax & Overview

T * restrict p

Promises that for the pointer's lifetime, the referenced memory is accessed only through pointers derived from p (no aliasing).

Notes & Pitfalls

Example

#include <assert.h>
#include <stddef.h>

static void add_vec(size_t n, int *restrict dst, const int *restrict src) {
    for (size_t i = 0; i < n; ++i) dst[i] += src[i];
}
int main(void) {
    int a[2] = {1, 2}, b[2] = {10, 20};
    add_vec(2, a, b);
    assert(a[0] == 11 && a[1] == 22);
}

Linked from

Related notes