restrict
In the C programming language, as of the C99 standard, restrict
is a keyword that can be used in pointer declarations. The restrict
keyword is a declaration of intent given by the programmer to the compiler. It says that only the pointer or a value based on the pointer (such as pointer+1) will be used to access the object it points to. This limits the effects of pointer aliasing, aiding optimization. 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:
void updatePtrs(size_t *ptrA, size_t *ptrB, size_t *val)
{
*ptrA += *val;
*ptrB += *val;
}
In the above code, the pointers ptrA
, ptrB
, val
might refer to the same memory location, so the compiler will generate a less optimal code :
load R1 ← *val ; Load the value of val pointer
load R2 ← *ptrA ; Load the value of ptrA pointer
add R2 += R1 ; Perform Addition
set R2 → *ptrA ; Update the value of ptrA pointer
; Similarly for ptrB, note that val is loaded twice,
; because ptrA may be equal to val.
load R1 ← *val
load R2 ← *ptrB
add R2 += R1
set R2 → *ptrB
However if the restrict
keyword is used and the above function is declared as :
void updatePtrs(size_t *restrict ptrA, size_t *restrict ptrB, size_t *restrict val);
then the compiler is allowed to assume that ptrA
, ptrB
, 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:
load R1 ← *val
load R2 ← *ptrA
load R3 ← *ptrB
add R2 += R1
add R3 += R1
set R2 → *ptrA
set R3 → *ptrB
Note that the above assembly code is better and the val
is loaded once.
Another example is of memcpy
. The two pointers used as arguments to memcpy(void*, void*, nbytes)
are declared with restrict
, which tells the compiler of the memcpy
function that the two data areas do not overlap. This allows the compiler to produce a faster memcpy
function. However, if a programmer uses the same pointer for both arguments, the behavior of the memcpy
function will be undefined.
References
- ISO/IEC 9899:TC2 Committee Draft. ISO. May 6, 2005. pp. 108–112. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf. Retrieved 2008-12-22.
External links
- Demystifying The Restrict Keyword: explanation and examples of use
- Walls, Douglas. "How to Use the restrict Qualifier in C". Sun Microsystems. http://developers.sun.com/solaris/articles/cc_restrict.html. Retrieved 2009-07-28.
If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...