Optimization
If the compiler knows that there is only one pointer to a memory block, it can produce better code. For instance:
void updatePtrs(size_t *ptrA, size_t *ptrB, size_t *val) { *ptrA += *val; *ptrB += *val; }In the above code, the pointers ptrA
, ptrB
, and val
might refer to the same memory location, so the compiler will 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 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:
load R1 ← *val load R2 ← *ptrA add R2 += R1 set R2 → *ptrA ; Note that val is not reloaded, ; because the compiler knows it is unchanged load R2 ← *ptrB add R2 += R1 set R2 → *ptrBNote that the above assembly code is shorter because val
is loaded once.
Read more about this topic: Restrict