restrict keyword — aliasing hint
WHAT is restrict?
- Aliasing = two different pointers refer to (overlap with) the same memory.
restrictis 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:
- load
b[i] - load
c[i] - 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
bandc, - compute several sums at once (SIMD),
- store the block of
a— order no longer matters.

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
restrictpointers actually overlap? - Which standard function relies on restrict-like semantics:
memcpyormemmove?
What kind of token is restrict in C?
const is a qualifier.What promise does T * restrict p make?
Does the compiler verify the restrict promise?
Why does restrict enable faster code?
What happens if two restrict pointers actually overlap?
Is restrict about modifiability?
const. restrict is purely an aliasing (non-overlap) hint.Which function has restrict-style 'no overlap' semantics, memcpy or memmove?
In for(i) v[i]*=*factor; why mark factor restrict?
What does the R.E.S.T. mnemonic stand for?
Connections
- Pointers in C
- const qualifier
- Pointer aliasing
- memcpy vs memmove
- Compiler optimization & vectorization (SIMD)
- Undefined behaviour in C
- C99 standard features
Concept Map
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.