Restrict keyword learning in c language

Source: Internet
Author: User

Brief introduction:

Restrict is introduced by the C99 standard, it can only be used to qualify and constrain pointers, and to indicate that pointers are 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 to the memory must be modified by that pointer, and not by other means ( Other variables or pointers) to modify; The advantage of this is that it helps the compiler to better optimize the code and generate more efficient assembly code. such as int *restrict ptr, ptr-pointed memory units can only be accessed by PTR, and any other pointers to the same memory unit are undefined. The straightforward point is the invalid pointer. Restrict is due to the inherent defects of C language itself, C programmers should proactively avoid this flaw, and the compiler will be very compatible with the optimization of your code.

Consider the following example:
int ar[10];
int * Restrict restar= (int *) malloc (10*sizeof (int));
int *par=ar;
This shows that Restar is the only and initial way to access the memory allocated by malloc (). Par is not.
So:
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.
This keyword is said to originate from the old Fortran. Interested in looking at this.

There are two functions in the

C library where you can copy bytes from one location to another. Under the C99 standard, their prototypes are as follows:
void * memcpy (void * restrict S1, const void * Restrict S2, size_t N);
void * Memove (void * S1, const void * s2, size_t N);
Both functions copy N-byte data from the location pointed to by the S2 to the S1 point, and both return S1 values. The difference between the two is caused by keyword restrict, that is, memcpy () can assume that two memory regions do not overlap. The Memmove () function does not make this assumption, so the replication process is similar to first copying all the bytes to a temporary buffer and then copying it to the final destination. What happens if you use memcpy () when two areas overlap. Its behavior is unpredictable, it can work correctly, or it may fail. The compiler does not prohibit the use of memcpy () when memcpy () should not be used. Therefore, when using memcpy (), you must ensure that there are no overlapping areas. This is part of the programmer's task. The
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.

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.