c中restrict关键字的理解
2013-05-04 22:04
447 查看
先看看restrict的用法,很简单 pointer_type * restrict arg 如(pthread_t * restrict tid)
为什么要引入restrict?
简单来讲就是为了让编译器进行优化。
One of the new features in the recently approved C standard C99, is the restrict pointer qualifier. This qualifier can be applied to a data pointer to indicate that, during the scope of that pointer declaration, all data accessed through it will be accessed
only through that pointer but not through any other pointer. The 'restrict' keyword thus enables the compiler to perform certain optimizations based on the premise that a given object cannot be changed through another pointer. Now you're probably asking yourself,
"doesn't const already guarantee that?" No, it doesn't. The qualifier const ensures that a variable cannot be changed through aparticular pointer. However, it's still possible to change the variable through a different pointer. For example:
void f (const int* pci, int *pi;); // is *pci immutable? { (*pi)+=1; // not necessarily: n is incremented by 1 *pi = (*pci) + 2; // n is incremented by 2 } int n; f( &n, &n);
In this example, both pci and pi point to the same variable, n. You can't change n's value through pci but you can change it using pi. Therefore, the compiler isn't allowed to optimize memory access for *pci by preloading n's value. In this example, the compiler
indeed shouldn't preload n because its value changes three times during the execution of f(). However, there are situations in which a variable is accessed only through a single pointer. For example:
FILE *fopen(const char * filename, const char * mode);
The name of the file and its open mode are accessed through unique pointers in fopen(). Therefore, it's possible to preload the values to which the pointers are bound. Indeed, the C99 standard revised the prototype of the function fopen() to the following:
FILE *fopen(const char * restrict filename, const char * restrict mode);
Similar changes were applied to the entire standard C library: printf(), strcpy() and many other functions now take restrict pointers:
int printf(const char * restrict format, ...); char *strcpy(char * restrict s1, const char * restrict s2);
C++ doesn't support restrict yet. However, since many C++ compilers are also C compilers, it's likely that this feature will be added to most C++ compilers too.
memcpy()在C99中,restrict可明确用于memcpy()的原型,而在C89中必须进行解释。
void *memcpy(void *restrict str1, const void *restrict str2, size_t size);
/*通过使用restrict修饰str1和str2来保证它们指向不重叠的对象*/
个人理解:restrict是programer对compiler的保证,即在restrict指针作用域中只有该指针会修改原始数据,因此编译器可以对其优化。如果没有满足这个条件的话,编译器优化很可能出错。
为什么要引入restrict?
简单来讲就是为了让编译器进行优化。
One of the new features in the recently approved C standard C99, is the restrict pointer qualifier. This qualifier can be applied to a data pointer to indicate that, during the scope of that pointer declaration, all data accessed through it will be accessed
only through that pointer but not through any other pointer. The 'restrict' keyword thus enables the compiler to perform certain optimizations based on the premise that a given object cannot be changed through another pointer. Now you're probably asking yourself,
"doesn't const already guarantee that?" No, it doesn't. The qualifier const ensures that a variable cannot be changed through aparticular pointer. However, it's still possible to change the variable through a different pointer. For example:
void f (const int* pci, int *pi;); // is *pci immutable? { (*pi)+=1; // not necessarily: n is incremented by 1 *pi = (*pci) + 2; // n is incremented by 2 } int n; f( &n, &n);
In this example, both pci and pi point to the same variable, n. You can't change n's value through pci but you can change it using pi. Therefore, the compiler isn't allowed to optimize memory access for *pci by preloading n's value. In this example, the compiler
indeed shouldn't preload n because its value changes three times during the execution of f(). However, there are situations in which a variable is accessed only through a single pointer. For example:
FILE *fopen(const char * filename, const char * mode);
The name of the file and its open mode are accessed through unique pointers in fopen(). Therefore, it's possible to preload the values to which the pointers are bound. Indeed, the C99 standard revised the prototype of the function fopen() to the following:
FILE *fopen(const char * restrict filename, const char * restrict mode);
Similar changes were applied to the entire standard C library: printf(), strcpy() and many other functions now take restrict pointers:
int printf(const char * restrict format, ...); char *strcpy(char * restrict s1, const char * restrict s2);
C++ doesn't support restrict yet. However, since many C++ compilers are also C compilers, it's likely that this feature will be added to most C++ compilers too.
memcpy()在C99中,restrict可明确用于memcpy()的原型,而在C89中必须进行解释。
void *memcpy(void *restrict str1, const void *restrict str2, size_t size);
/*通过使用restrict修饰str1和str2来保证它们指向不重叠的对象*/
个人理解:restrict是programer对compiler的保证,即在restrict指针作用域中只有该指针会修改原始数据,因此编译器可以对其优化。如果没有满足这个条件的话,编译器优化很可能出错。
相关文章推荐
- C关键字restrict的理解
- c语言中restrict关键字的理解
- 深入理解C++中关键字 mutable
- 深入理解Java中的final关键字
- 关于java synchronized关键字的一些理解
- c99 增加的restrict关键字
- 对volatile关键字的理解
- 关键字explicit理解
- 对java中static关键字的理解
- 指针常量C语言关键字const再理解
- 《C语言深度解剖》signed,unsigned关键字后面3个问题的理解
- Java final关键字的理解
- 从java虚拟机层次理解this和static关键字
- 深入理解C++中的mutable关键字
- 全面理解Java内存模型(JMM)及volatile关键字
- Python 中的黑暗角落(一):理解 yield 关键字
- 深入理解Java中的final关键字
- JAVA static 关键字的理解
- 理解static关键字
- static关键字理解整理:静态变量、静态函数的作用及使用规则