Restrict
Encyclopedia
In the C programming language, as of the C99 standard
,
that can be used in pointer declarations. The
. It says that for the lifetime of the pointer, only it or a value directly derived from it (such as ) will be used to access the object to which it points. This limits the effects of pointer aliasing, aiding caching optimizations. If the declaration of intent is not followed and the object is accessed by an independent pointer, this will result in undefined behavior.
The following hypothetical example makes it clearer:
In the above code, the pointers
, so the compiler will generate a less optimal code :
However if the
then the compiler is allowed to assume that
Now the compiler can generate better code as follows:
Note that the above assembly code is shorter because
C99
C99 is a modern dialect of the C programming language. It extends the previous version with new linguistic and library features, and helps implementations make better use of available computer hardware and compiler technology.-History:...
,
restrict
is a keywordKeyword (computer programming)
In computer programming, a keyword is a word or identifier that has a particular meaning to the programming language. The meaning of keywords — and, indeed, the meaning of the notion of keyword — differs widely from language to language....
that can be used in pointer declarations. The
restrict
keyword is a declaration of intent given by the programmer to the compilerCompiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...
. It says that for the lifetime of the pointer, only it or a value directly derived from it (such as ) will be used to access the object to which it points. This limits the effects of pointer aliasing, aiding caching optimizations. If the declaration of intent is not followed and the object is accessed by an independent pointer, this will result in undefined behavior.
Optimization
If the compiler knows that there is only one pointer to a memory block, it can produce better code.The following hypothetical example makes it clearer:
In the above code, the pointers
ptrA
, ptrB
, and val
might refer to the same memory locationPointer alias
In computer programming, aliasing refers to the situation where the same memory location can be accessed using different names.For instance, if a function takes two pointers A and B which have the same value, then the name A[0] aliases the name B[0]. In this case we say the pointers A and B alias...
, so the compiler will generate a 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
ptrA
, ptrB
, and val
point to different locations and updating one pointer will not affect the other pointers. The programmer, not the compiler, is responsible for ensuring that the pointers do not point to identical locations.Now the compiler can generate better code as follows:
Note that the above assembly code is shorter because
val
is loaded once.External links
- Demystifying The Restrict Keyword: explanation and examples of use