restrict C99 restricted pointers
restrict C99 restricted pointers
C99's restrict promises exclusive object access during association, enabling optimization. Overlaps violating this promise cause undefined behavior.
| Category | Type qualifiers |
|---|---|
| Kind | Keyword / Statement |
| C standard | C99 |
📝 Syntax
void copy(int * restrict dst, const int * restrict src, int n);
▶ Example
Edit the C code below and click Run; the server compiles and executes it:
Output:
2 4 6
🏷️ Related type qualifiers
📚 Related Tutorials
❓ FAQ
QWhat happens when overlapping memory is passed to restrict pointers?
AIf the accesses violate restrict's exclusive-access contract, the program has undefined behavior; the compiler does not check overlap for the caller. Use memmove, whose semantics support overlap, rather than this example's interface for potentially overlapping buffers.
QDoes restrict check pointer overlap at runtime?
ANo. It is a semantic promise from the programmer to the compiler that enables optimization, with no runtime detection. The caller must ensure that every invocation satisfies the contract.
QIs it a violation for two restrict pointers only to read the same object?
AIf the object is not modified during the relevant period, reads through two restricted pointers alone do not create this exclusivity conflict. If one pointer also modifies the object, aliased accesses may violate the promise, so analyze the complete access sequence.