The restrict modifier pointer is added in C99:
The Pointer Modified by restrict is the first and only method to access the object pointed to by the pointer,
The object can be accessed only when the second pointer is based on the first pointer.
Access to objects is limited to pointer expressions modified by restrict.
Pointers modified by restrict are mainly used for function parameters or memory space allocated by malloc.
The restrict data type does not change the semantics of the program.
The compiler can better optimize some types of routines by making the restrict modifier pointer as the assumption that the only method to access the object is used.
Restrict is introduced by the c99 standard. It can only be used to limit and constrain pointers,
The pointer is the only and initial method to access a data object.
That is, it tells the compiler that all operations that modify the content pointed to by the pointer to the memory must be modified using this pointer,
It cannot be modified through other channels (other variables or pointers). The advantage of doing so is that,
It can help the compiler to better optimize code and generate more efficient assembly code. For example:
Int * restrict ptr,
The memory unit pointed to by ptr can only be accessed by ptr, and any other pointer pointing to this memory unit is undefined,
To put it bluntly, the pointer is invalid.
Restrict occurs because of the inherent defects of the C language,
C programmers should take the initiative to circumvent this defect, and the compiler will work very well to optimize your code.
Example:
Copy codeThe Code is as follows: int ar [10];
Int * restrict restar = (int *) malloc (10 * sizeof (int ));
Int * par = ar;
For (n = 0; n <10; n ++)
{
Par [n] + = 5;
Restar [n] + = 5;
Ar [n] * = 2;
Par [n] + = 3;
Restar [n] + = 3;
}
Because restar is the only and initial way to access the allocated memory, the compiler can optimize the restar operation as described above:
Restar [n] + = 8;
The par is not the only way to access the array ar, so the following optimization cannot be performed:
Par [n] + = 8;
Because before par [n] + = 3, ar [n] * = 2 is changed.
With the keyword restrict, the compiler can be optimized with confidence.
The keyword restrict has two readers.
One is the compiler, which tells the compiler to freely make optimization assumptions.
Another reader is a user who tells the user to only use parameters that meet the restrict requirements.
Generally, the compiler cannot check whether you have followed this restriction. If you despise it, you are taking a risk.
To help the compiler determine memory dependencies,
You can qualify a pointer, reference, or array
With the restrict keyword.
The restrict keyword is a type qualifier that may be
Applied to pointers, references, and arrays.
Its use represents a guarantee by the programmer
That within the scope of the pointer declaration
The object pointed to can be accessed only by that pointer.
Any violation of this guarantee renders the program undefined.
This practice helps the compiler optimize certain sections of code
Because aliasing information can be more easily determined.
Use of the restrict type qualifier with pointers
Copy codeCode: void func1 (int * restrict a, int * restrict B)
{
/* Func1's code here */
}
In the example that follows, the restrict keyword is
Used to tell the compiler that the function func1 is
Never called with the pointers a and B pointing
To objects that overlap in memory.
You are promising that accesses through a and B
Will never conflict; this means that a write through one pointer
Cannot affect a read from any other pointer.
The precise semantics of the restrict keyword are
Described in the 1999 version of the iso c standard.
Use of the restrict type qualifier with arrays
Copy codeThe Code is as follows: void func2 (int c [restrict], int d [restrict])
{
Int I;
For (I = 0; I <64; I ++)
{
C [I] + = d [I];
D [I] + = 1;
}
}
This example extends strates using the restrict keyword when passing arrays to a function.
Here, the arrays c and d shoshould not overlap, nor shoshould c and d point to the same array.