The use of restrict keywords in C language _c language

Source: Internet
Author: User

A new restrict-decorated pointer is added to the C99:

A pointer decorated by restrict is the first way to access the object that the pointer points to.
An object can be accessed only if the second pointer is based on the first.
Access to an object is limited to a pointer expression that is based on a restrict modification.

Pointers decorated by restrict are primarily used for function parameters or point to the memory space allocated by malloc ().
The Restrict data type does not change the semantics of the program.
The compiler can better optimize certain types of routines by making restrict-modified pointers the only way to access objects.


Restrict is introduced by the C99 standard, which can only be used to qualify and constrain pointers,
and indicates that the pointer is the only and initial way to access a data object.
That is, it tells the compiler that all operations that modify the contents of the pointer in memory must be modified by the pointer.
It cannot be modified by other means (other variables or pointers); The advantage of doing so is that
can help the compiler to better optimize the code and generate more efficient assembly code.

int *restrict ptr,

A PTR-pointed memory unit can only be accessed by PTR, and any other pointers to the same memory unit are undefined.
The straightforward point is the invalid pointer.

The appearance of restrict is due to the inherent defects of C language itself,
C programmers should proactively circumvent this flaw, and the compiler will optimize your code in a very coordinated way.

Example:

Copy Code code 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 allocated memory, the compiler can optimize the operation of the Restar:
restar[n]+=8;


Par is not the only way to access array AR, so the following optimizations are not possible:
par[n]+=8;


Because before the par[n]+=3, the ar[n]*=2 changed.
Using the keyword restrict, the compiler can be assured of optimization.

Keyword restrict has two readers.
One is the compiler, which tells the compiler to be free to do some assumptions about optimization.
Another reader is a user who tells the user to use only the parameters that meet the restrict requirements.

In general, the compiler cannot check whether you have followed this limitation, and if you despise it you are risking yourself.
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
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 is accessed only by that pointer.

Any violation of this guarantee renders is the program undefined.
This practice helps the compiler optimize certain sections of code
Because aliasing information can be more easily determined.

Use the Restrict type qualifier with pointers

Copy Code code as follows:

void func1 (int * Restrict A, int * restrict B)
{
* func1 ' s code here/*
}

In the example so follows, the Restrict keyword is
Used to tell the compiler this function func1 is
Never called with the pointers A and B pointing
to objects which overlap in memory.
Are promising that accesses through a and B
would never conflict; This is means that a write through one pointer
Cannot affect a read from the other pointer.
The precise semantics of the Restrict keyword are
Described in the 1999 version of the ISO C standard.

Use the Restrict type qualifier with arrays

Copy Code code as follows:

void Func2 (int c[restrict], int d[restrict])
{
int i;

for (i = 0; i < i++)
{
C[i] + = D[i];
D[i] + + 1;
}
}


This example illustrates using the Restrict keyword when passing to a function.
Here, the arrays C and D-should not overlap, nor should C and D-to the same array.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.