restrict
restrict is a pointer qualifier that promises the object it points at will be accessed, for the lifetime of the pointer, only through that pointer and values derived from it. The compiler can then assume the object does not alias anything else.
Detailed Explanation
Aliasing — two pointers addressing overlapping objects — forces the compiler to reload from memory more often than you would like. After *a = 1;, a store through *b might have changed *a, so the next read of *a cannot use the constant 1 unless the compiler proves a and b are distinct.
restrict, added in C99, is that proof, supplied by you. void *memcpy(void *restrict dest, const void *restrict src, size_t n); means dest and src do not overlap. The compiler may load all of src, then store all of dest, or vectorize the copy. If they do overlap, the call is undefined behavior. That is why memmove exists and does not use the same contract.
The promise is not "these two parameters are different." It is "all access to this object goes through this pointer." A function that writes through restrict dest and also writes the same bytes through a global pointer has broken the contract even if no other parameter aliases dest.
restrict is a contract, not a check. The compiler does not verify it. Breaking it is undefined behavior: you may see a correct result today and a wrong one after an optimizer upgrade. Use it where the overlap rule is already part of the API — memcpy, a destination buffer the caller owns exclusively, an output array the function fills from a disjoint input. Do not sprinkle it on every pointer "for speed." The wins appear in tight numeric loops. They do not appear in typical business logic.
C++ does not have restrict in the standard. Compilers offer __restrict or __restrict__ as an extension. A header shared with C++ should hide the qualifier behind a macro.
A restrict pointer can still be null if the function permits it. The qualifier does not imply non-null, and it does not transfer ownership.
Code Examples
#include <stdio.h>
#include <stddef.h>
/* dest and src must not overlap. The compiler may vectorize the add. */
static void add_into(size_t n,
double *restrict dest,
const double *restrict src)
{
for (size_t i = 0; i < n; i++) {
dest[i] += src[i];
}
}
int main(void)
{
double a[] = { 1.0, 2.0, 3.0 };
double b[] = { 0.5, 0.5, 0.5 };
add_into(3, a, b);
printf("%.1f %.1f %.1f\n", a[0], a[1], a[2]); /* prints: 1.5 2.5 3.5 */
return 0;
}#include <stdio.h>
#include <string.h>
int main(void)
{
char buf[] = "abcdef";
/* memcpy(buf + 1, buf, 3) would be undefined: dest and src overlap. */
memmove(buf + 1, buf, 3); /* defined overlap */
printf("%s\n", buf); /* prints: aabcde */
return 0;
}Common Gotchas
Overlapping `restrict` pointers is undefined behavior
the bug can be a silently wrong numeric result, not a crash. Use `memmove` or drop `restrict` when overlap is possible.
The contract covers every access, including globals and other parameters
a helper that writes the same object through a second pointer breaks `restrict` on the first.
`restrict` does not mean "not null" and does not mean "unique owner"
it is only an aliasing promise for the duration of the pointer.
C++ code cannot use standard `restrict`
wrap it in a macro so the header stays valid in both languages.