ADocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
restrict-qualified pointer is a type qualifier introduced in C99 that asserts a strict non-aliasing guarantee to the compiler. By applying restrict to a pointer, the programmer explicitly promises that for the lifetime of that pointer, if the memory object it references is modified, all accesses to that object (both reads and writes) will occur exclusively through that pointer or pointers derived directly from it.
Type Constraints
Therestrict qualifier can only be applied to pointers that point to object types. Because incomplete types (such as void or forward-declared structs) are formally classified as a subset of object types in the C Standard, they are inherently supported. It is a constraint violation to apply restrict to function pointers.
Syntax and Contexts
Therestrict keyword qualifies the pointer itself, not the underlying type. In standard pointer declarations, it is placed after the asterisk (*). However, when applying the qualifier to a typedef of a pointer type, it is placed directly after the type name. Additionally, C99 introduced a specific syntax for array parameters in function declarations, allowing restrict to be placed inside the array brackets.
Core Semantics and Scope
Therestrict qualifier establishes a formal contract regarding pointer aliasing. The rules of this contract are bound to the pointer’s associated block (C11 6.7.3.1). The associated block is determined by how the pointer is declared or designated:
- Block Scope and Parameters: If the pointer is declared inside a block or as a function parameter, the associated block is the block in which the declaration occurs (or the function body for parameters).
- File Scope: If the pointer is declared at file scope (as a global), its associated block is defined by the standard as the block of
main, meaning the non-aliasing promise applies to the entire execution of the program. - Struct Members: If
restrictis applied to a member of astructorunion, the associated block is determined by the declaration of the ordinary identifier used to designate the object containing the member. (Note: As highlighted in C Defect Report 460, the standard’s formal definition is ambiguous regarding dynamically allocated objects that lack an ordinary identifier, but the semantics strictly rely on the lifetime of the identifier used to access the object).
- Exclusive Access on Modification: If the memory object is modified, all accesses to that object (both reads and writes) must occur through the
restrictpointer. - Derivation: Pointers derived from a
restrictpointer (e.g., via pointer arithmetic likeptr + 1or array indexingptr[i]) are covered by the same guarantee and are legally allowed to access the memory. - Read-Only Exception: If the memory object is never modified during the pointer’s lifetime, the
restrictqualifier is effectively ignored, and multiple pointers can safely read from the same location.
Compiler Implications
The primary purpose ofrestrict is to enable aggressive compiler optimizations that are otherwise blocked by the possibility of aliasing.
Without restrict, the compiler must assume that writing to one pointer might alter the value read by another pointer. This forces the compiler to emit defensive machine code:
- Flushing registers to memory.
- Reloading values from memory before subsequent reads.
- Preserving strict, sequential instruction ordering.
restrict permits the compiler to:
- Cache values in registers: The compiler knows the underlying memory will not be mutated by a side channel.
- Reorder instructions: Independent loads and stores can be scheduled optimally for the CPU pipeline.
- Vectorize operations: The compiler can safely emit SIMD (Single Instruction, Multiple Data) instructions without emitting runtime checks for overlapping memory boundaries.
Undefined Behavior
Becauserestrict is a programmer-enforced promise, the compiler does not verify it at compile time. If the contract is violated—meaning a restrict-qualified pointer’s target is modified, and that same memory is accessed via an independent, non-derived pointer within the pointer’s associated block—the program invokes Undefined Behavior (UB).
p1 and p2 point to disjoint memory regions.
Master C with Deep Grasping Methodology!Learn More





