Common · Knowledge
Restrict
C keyword used in pointer declarations to assert that pointer aliasing does not occur
In the C programming language, restrict is a type qualifier that can be applied to a pointer to hint to the compiler that for the lifetime of that pointer, no other pointer will be used to access the same pointed-to object. This limits the effects of pointer aliasing, which can allow the compiler to make optimizations (such as vectorization) that would not otherwise be possible.
Read more
Undefined behaviour results if the declaration of intent is not followed and the object is accessed by an independent pointer. The restrict keyword was introduced by the C99 standard. Despite C++ supporting most features of C, restrict is not included in standard C++. If the compiler knows that there is only one pointer to a memory block, it can produce better optimized code. For instance, in a function updatePointers() which adds a value x to both its arguments a and b: In the above code, the pointers a, b, and x might refer to the same memory location, so the compiler may generate less optimal code: However, if the restrict keyword is used and the above function is declared as then the compiler is allowed to assume that a, b, and x point to different locations and updating the memory location referenced by one pointer will not affect the memory locations referenced by the other pointers. The programmer, not the compiler, is responsible for ensuring that the pointers do not point to identical locations. The compiler can e.g. rearrange the code, first loading all memory locations, then performing the operations before committing the results back to memory. The above assembly code is shorter because x is loaded only once.
Text: Wikipédia, CC BY-SA 4.0. ·