On this page
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
- Promise to the compiler that two pointers do NOT alias (C99): Unlocks vectorization and eliminates redundant loads—violating it (passing overlapping buffers) is UB.
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
- tuts/c/builtins/inline Linked here; links to this page; shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/volatile Linked here; links to this page; shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins Linked here; links to this page; shared tags: #builtins, #c, #prog
- tuts/c/compilation_and_internals/memory_layout_and_abi Linked here; shared tags: #c, #prog
- tuts/c/builtins/__FILE__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/__func__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/__LINE__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/__STDC_VERSION__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/_Alignof shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/_Atomic shared tags: #builtins, #c, #prog, #tier