Register, volatile, and restrict keywords

Source: Internet
Author: User

Register

Variables declared using the modifier register belong to the Register storage type. This type is similar to the automatic storage type, with automatic storage period,CodeBlock scope and inner connection. Declared as register is only a request, so this variable may still be a common automatic variable. In either case, the address cannot be obtained for the variable modified with register. If it is not initialized, its value is not fixed.

 

Volatile
Volatile tells the CompilerProgramIt may be modified by other proxies or threads. Therefore, when using the value of a variable declared by volatile, the system always reads data from its memory instead of using the cached value in the register. For example,

Val1 = X;
Val2 = X;

If volatile is not declared, the system may directly read X from the register rather than from the initial location of the memory when assigning values to val2. Between two values, X may be completely changed by some unknown factors of the compiler (such as the operating system, hardware, or other threads ). If declared as volatile, the compiler will not use the cache, but re-read X from the memory each time.

Restrict

Restrict is introduced by c99. It can only be used to restrict pointers and indicates that pointers are the only and initial method for accessing a data object. 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:

 
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 restar operation: 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 restric, the compiler can be optimized with confidence. This keyword is said to have come from the ancient FORTRAN.

 

Summary

Two keywords: volatile and restrict are used to facilitate Compiler optimization.

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.