A pointer is the only and initial method for accessing a data object.

Source: Internet
Author: User

Restrict is introduced by c99. It can only be used to restrict pointers and indicates that pointers are the only and initial method to access a data object. the object can be accessed only when the second pointer is based on the first pointer. consider the following example: int ar [10]; int * restrict restar = (int *) malloc (10 * sizeof (int); int * par = ar; the restar is the only and initial method to access the memory allocated by malloc. No. So: [cpp] 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, then the compiler can optimize the restar operation: restar [n] + = 8; and 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 restric, the compiler can be optimized with confidence. Void * memcpy (void * restrict s1, const void * restrict s2, size_t n); if the copy happens between two overlapping objects, the behavior is uncertain. Void * memmove (void * s1, constvoid * s2, size_t n); Copying is not affected even if the two pointers point to different regions. It is worth noting that once you decide to use restrict to modify pointers, you must ensure that they do not overlap with each other and the compiler will not check for you. Void setbuf (FILE * restrict fp, char * restrict buf); indicates that the keyword "memory space not exceeded by fp and buf" is related to optimization. For example, www.2cto. comvoid func (int * a, int * B) {* a + = * B;} can be optimized to void func (int *, int * B) {* a + = * B <1;} but if a and B point to the same integer, the semantics of the two functions is different. Because of this risk, many compilers will not be so optimized. Now, with the restrict keyword added, the compiler can perform this optimization boldly. In addition, for optimization, the restrict and register keywords are built upon, while the volatile keywords are mandatory.

Related Article

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.