5.1.32 · D3C Programming

Worked examples — restrict keyword — aliasing hint

3,510 words16 min readBack to topic

This page is the exhaustive drill. First we lay out a matrix of every kind of overlap situation C can hand you; then we solve one example per cell.

See the parent for the theory: restrict — aliasing hint.


Two words we must pin down first

Before any example, two terms and one abbreviation — because the whole page hangs on telling them apart.


One more idea: scalar vs. vectorized ("SIMD") builds

Several examples below say a result "differs between a scalar build and a vectorized build." Let's earn those two words before we use them.

Whenever an example shows "scalar gives X, vectorized gives Y, and X ≠ Y", that disagreement is the proof of undefined behaviour.


The scenario matrix

Now the picture. Two arrays live in a flat strip of memory. Each pointer names a start address and (through how many elements you touch) a range of bytes.

Figure — restrict keyword — aliasing hint

Figure s01 (described). A horizontal strip of eight numbered memory cells (indices 0–7). A black arrow beneath spans cells 2–5 labelled "pointer p range"; a black arrow above spans cells 0–3 labelled "pointer q range". The two cells they both cover (2 and 3) are outlined in red and labelled "shared bytes (danger zone)". The red outline is the overlap.

If the red zone is empty, the promise can be kept. If it has even one byte, the promise is broken the moment both pointers touch it.

Here is every case-class this topic can throw at you:

Cell Situation Regions overlap? restrict legal?
A Two separate arrays, different objects No ✅ Yes
B Forward-shifted copy dst = src+k, k>0 Yes (partial) ❌ No
C Backward-shifted copy dst = src-k, k>0 Yes (partial) ❌ No
D Exact same pointer dst == src, n>0 Fully (a self-alias) ❌ No
E Adjacent, touching but not overlapping (dst = src+n) No (edge case!) ✅ Yes
F Scalar pointer possibly inside an array (factor in v) Depends ✅ only if outside
G Zero-length / degenerate (n = 0) Vacuously none ✅ Yes (no accesses)
H Real-world word problem (image row blur) Must be checked depends
I Exam twist: const + restrict alias, overlap only visible at -O3 Yes (full) ❌ No

We now walk every cell, in table order A → I — one worked example each.


The two functions used below

Examples 1–4 call two tiny functions. Here are their signatures so nothing is used undefined:


Worked examples










Recap of the matrix

Recall

Every cell, one line each. Cell A (two arrays) legal? ::: Yes — disjoint, deterministic, vectorizable. Cell B (dst=src+1) legal? ::: No — partial overlap [1,4), UB. Cell C (dst=src-1) legal? ::: No — still overlaps; a "correct-looking" scalar result is luck. Cell D (dst==src, n>0) legal? ::: No — full self-alias; two restrict params name the same bytes ⇒ UB, even though the values come out unchanged. Cell E (dst=src+n) legal? ::: Yes — adjacent, no shared byte (src_end ≤ dst_start, non-strict). Cell F (scalar inside array) legal? ::: Only if the scalar lives outside the array. Cell G (n=0) legal? ::: Yes — zero accesses can't violate the promise, even if dstsrc. Cell H (in-place blur, outin) legal? ::: No — full overlap; use a separate output buffer. Cell I (const+restrict alias) legal? ::: No — const doesn't stop the aliasing; -O0 and -O3 disagree ⇒ UB.


Connections

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