5.1.32C Programming

restrict keyword — aliasing hint

1,632 words7 min readdifficulty · medium

WHAT is restrict?

  • Aliasing = two different pointers refer to (overlap with) the same memory.
  • restrict is an anti-aliasing hint: it asserts no aliasing through that pointer.
  • Breaking the promise → undefined behaviour. The compiler trusts you blindly.

WHY does it exist?

Derivation-from-scratch: where the cost comes from

Consider:

void add(int *a, int *b, int *c, int n) {
    for (int i = 0; i < n; i++)
        a[i] = b[i] + c[i];
}

The compiler cannot know that a doesn't overlap b or c. If a == b+1, then writing a[i] changes b[i+1]. So the compiler is forced to:

  1. load b[i]
  2. load c[i]
  3. store into a[i] …strictly in that order, every iteration, no vectorization, because a store might invalidate a future load.

Now mark them:

void add(int * restrict a, const int * restrict b,
         const int * restrict c, int n) {
    for (int i = 0; i < n; i++)
        a[i] = b[i] + c[i];
}

You promised a, b, c don't overlap. Compiler can now:

  • prefetch a block of b and c,
  • compute several sums at once (SIMD),
  • store the block of a — order no longer matters.
Figure — restrict keyword — aliasing hint

HOW to use it


Common mistakes (Steel-man)


Recall Feynman: explain to a 12-year-old

Two robots are each given a list to write numbers into. If you tell them "your two notebooks are totally different notebooks," they can write super fast without checking each other. But if you secretly hand them the same notebook while claiming they're different, they scribble over each other and you get garbage. restrict is you promising the robots their notebooks are different — and if you lie, the result is junk.


Active recall

Recall Quick self-test
  • What category of token is restrict? (qualifier? keyword?)
  • Does the compiler verify your restrict promise?
  • What happens if two restrict pointers actually overlap?
  • Which standard function relies on restrict-like semantics: memcpy or memmove?
What kind of token is restrict in C?
A type qualifier (C99) applied to pointers, like const is a qualifier.
What promise does T * restrict p make?
That for p's lifetime, the pointed-to object is accessed only through p (or pointers derived from p) — i.e. no aliasing.
Does the compiler verify the restrict promise?
No. It trusts you; breaking it is undefined behaviour.
Why does restrict enable faster code?
It removes the compiler's worst-case assumption that pointers may alias, allowing reordering, caching values in registers, and vectorization.
What happens if two restrict pointers actually overlap?
Undefined behaviour — results may be wrong, often only at higher optimization levels.
Is restrict about modifiability?
No, that's const. restrict is purely an aliasing (non-overlap) hint.
Which function has restrict-style 'no overlap' semantics, memcpy or memmove?
memcpy (overlap is UB); memmove explicitly allows overlap.
In for(i) v[i]*=*factor; why mark factor restrict?
So the compiler knows factor doesn't point inside v, letting it load *factor once into a register instead of re-reading each iteration.
What does the R.E.S.T. mnemonic stand for?
Regions Entirely Separate, Trust me.

Connections

  • Pointers in C
  • const qualifier
  • Pointer aliasing
  • memcpy vs memmove
  • Compiler optimization & vectorization (SIMD)
  • Undefined behaviour in C
  • C99 standard features

Concept Map

is a

asserts

negates

if broken

forces

causes

blocks

removes

enables

requires

uses restrict semantics

no promise

restrict qualifier C99

Programmer promise

Aliasing overlap

No-aliasing hint

Undefined behaviour

Worst-case assumption

Extra re-loads

Vectorization SIMD

Disjoint memory regions

memcpy

memmove

Hinglish (regional understanding)

Intuition Hinglish mein samjho

restrict ek aisa keyword hai jo tum compiler ko ek promise dete ho: "is pointer ke through jo memory access ho rahi hai, wahi memory kisi aur alag pointer se overlap nahi karti." Matlab do pointer kabhi same jagah point nahi karenge. Yeh promise tum dete ho — compiler check nahi karta. Agar tumne jhooth bola (regions sach mein overlap karte hain), to result undefined behaviour hoga, jo aksar -O2 ya -O3 pe hi galat dikhega.

Iska faayda kya hai? Bina restrict ke, compiler ko worst case maan na padta hai — ki shayad a aur b same memory ho. Isliye loop mein har baar memory se dobara value load karni padti hai, aur vectorization (SIMD) nahi ho pati. Restrict lagane se compiler nishchint ho jaata hai, value ko register mein cache kar leta hai, aur multiple operations ek saath kar leta hai — code tezz ho jaata hai.

Yaad rakho: restrict ka matlab "read-only" nahi hai (wo const ka kaam hai), aur na hi yeh khud se safety check karta hai. Yeh sirf aliasing hint hai. Classic example: memcpy restrict-style semantics use karta hai isliye overlapping memory pe memcpy UB hai, par memmove safe hai kyunki wo overlap allow karta hai. Mnemonic — R.E.S.T.: Regions Entirely Separate, Trust me.

Go deeper — visual, from zero

Test yourself — C Programming

Connections