关于volatile和restrict关键字

原帖连接 http://hi.baidu.com/phps/blog/item/dc1ce3cde42bff520eb345e8.html

volatile 可能我们用的都比较多也比较常见吧,主要就是告诉编译器,每次在使用volatile指定的变量时总是重新去获取他的值,更简单的理解我是这样的,
为了计算某个内容地址所存的内容会把他放入CPU寄存器,为了优化,下一次就直接从寄存器里取值了
volatile就是为了告诉编译器,不管什么情况你都要去内存里重新获取他的内容!!嘻嘻*^_^*

restrict就比较少见了。
restrict说明这个变量没有别的指针可以修改,让编译器生成只读内存一次的代码。

'Restrict' Pointers
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 a particular 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:

 

 
/* new declaration of fopen() in <stdio.h> */
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.


c99中新增加了一个类型定义,就是restrict。
restrict 的定义是It can be applied only to pointers, and it indicates that a pointer is the sole initial means of accessing a data object.
我不知道确切应该怎么翻译,大意是restrict只对指针有用,它声明一个指针是唯一初始化访问一个数据对象。
比如,按照书上的例子,
   
   
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;
}


restar指针是restrict类型,par指针就不是,因为par即没有初始化也不是唯一访问ar数组的变量。
那么,上面的程序,因为restar是唯一反问数据块的指针,所以编译器可以对它优化为一条语句,
restar[n] += 8;    /* ok replacement */
而par就不可以,
par[n] += 8;     / * gives wrong answer */

但要注意:restrictC99中新增的关键字,在C89C++中都不支持,在gcc中可以通过—std=c99来得到对它的支持。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值