liveBook · Manning

Restrict

As explained in Modern C

restrict is a C type qualifier for pointer types that declares an intended exclusive access relationship for an object during a computation.

The library distinction

```c void *memcpy(void *restrict s1, const void *restrict s2, size_t n); void *memmove(void *s1, const void *s2, size_t n); ``` `memcpy` requires the objects accessed through `s1` and `s2` not to overlap. `memmove` makes no such assumption and therefore accepts overlapping objects, potentially with less opportunity for optimization. TAKEAWAY: `restrict` promises exclusive access through a pointer; the caller must ensure that promise holds. section 15.2

`restrict` is a qualifier for pointer types. It tells the compiler that, during the relevant computation, the qualified pointer provides the exclusive access to the object it points to. The object must not also be accessed through an alias handled by the compiler. This can enable optimization, but the declaration constrains the caller rather than enforcing the property for them. section 15.2 section 15.4

Apply the rule carefully at function boundaries:

  • Treat two restrict-qualified parameters as requiring separate, non-overlapping access when the function execution uses both. section 15.2
  • Check variadic calls as well: a format pointer must not alias arguments passed through the `...` portion of `printf`-family functions. section 15.2
  • Do not assume a legal cast removes the obligation; accessing the same object through another pointer can still violate the restriction. section 15.2
  • Measure claimed gains rather than relying on the qualifier alone, because incorrect use trades correctness for an optimization opportunity. section 15.4
1   void* memcpy(void*restrict s1, void const*restrict s2,  size_t n);
2   void* memmove(void* s1, const void* s2, size_t n);


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":1}],[{\"line\":1,\"ch\":25},{\"line\":1,\"ch\":26}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":8}],[{\"line\":1,\"ch\":4},{\"line\":1,\"ch\":8}],[{\"line\":0,\"ch\":10},{\"line\":0,\"ch\":16}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":8}],[{\"line\":1,\"ch\":4},{\"line\":1,\"ch\":8}],[{\"line\":0,\"ch\":22},{\"line\":0,\"ch\":30}],[{\"line\":0,\"ch\":35},{\"line\":0,\"ch\":45}],[{\"line\":0,\"ch\":22},{\"line\":0,\"ch\":30}],[{\"line\":0,\"ch\":60},{\"line\":0,\"ch\":66}],[{\"line\":1,\"ch\":44},{\"line\":1,\"ch\":50}],[{\"line\":0,\"ch\":56},{\"line\":0,\"ch\":57}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":1}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":8}],[{\"line\":1,\"ch\":4},{\"line\":1,\"ch\":8}],[{\"line\":1,\"ch\":10},{\"line\":1,\"ch\":17}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":8}],[{\"line\":1,\"ch\":4},{\"line\":1,\"ch\":8}],[{\"line\":1,\"ch\":28},{\"line\":1,\"ch\":38}],[{\"line\":0,\"ch\":60},{\"line\":0,\"ch\":66}],[{\"line\":1,\"ch\":44},{\"line\":1,\"ch\":50}]]"}
!@%STYLE%@!

Compare the declarations of `memcpy` and `memmove` directly to see where exclusive-access assumptions appear in an interface. section 15.2

How Modern C covers it

How deep it goes
Defines exclusive access, caller obligations, aliasing consequences, and library contrasts, but leaves full language mechanics deferred.
Examples
Uses memcpy versus memmove, formatted-output declarations, and calls that alias format strings or stream objects.
What it assumes
Assumes familiarity with pointers, function parameters, and aliasing while emphasizing that callers verify the restriction.
What you can do after
Apply it via C library and user-defined function interfaces while checking that pointer accesses remain exclusive.
How it connects
Connects restrict with pointers, aliasing, function parameters, memcpy, memmove, and variadic functions.

Also explained in

Compare how 2 books explain restrict →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.