Chinaunix首页 | 论坛 | 博客
  • 博客访问: 207429
  • 博文数量: 127
  • 博客积分: 1998
  • 博客等级: 上尉
  • 技术积分: 1432
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-07 16:41
文章分类

全部博文(127)

文章存档

2014年(41)

2013年(1)

2012年(85)

分类: C/C++

2012-04-19 16:33:09

'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:

  1.    void f (const int* pci, int *pi;); // is *pci immutable?
  2.    {
  3. (*pi)+=1; // not necessarily: n is incremented by 1
  4.     *pi = (*pci) + 2; // n is incremented by 2
  5.    }
  6.    int n;
  7.    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:


  1.    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 ; */
   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:

  1.    int printf(const char * restrict format, ...);
  2.    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.

Danny Kalev
Ashdod, Israel  

--------------------------------------------------------------------------------
If you have a hot tip and we publish it, we'll pay you $10. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit a tip here.

void *memcpy(void * restrict s1,const void * restrict s2,size_t n);
如果拷贝发生在两个重叠的对象之间,行为是不确定的。
void *memmove(void *s1, constvoid *s2, size_t n);
即使两个指针指向的区域互相重叠,拷贝也不会受影响。

值得注意的是,一旦你决定使用restrict来修饰指针,你必须得保证它们之间不会互相重叠,编译器不会替你检查。

 

 

void setbuf (FILE *restrict fp,char *restrict buf);
表示fp和buf所指内存空间不交叉
这个关键字与优化有关。
比方
void func(int*a,int*b)
{
    *a+=*b;
    *a+=*b;
}
本来可以优化为
void func(int*a,int*b)
{
    *a+=*b<<1;
}
可是如果a和b指向同一整型,两个函数语意就不一样了。
因为有这种风险,于是很多编译器便不会这么优化了。
现在加了restrict关键字,编译器就可以大胆的做这种优化了
另外,对于优化而言,restrict,register关键字是建议性的,而volatile关键字是强制性的

 

 

 

 

简单例子:
  int ar[10];
  int * restrict restar=(int *)malloc(10*sizeof(int));
  int *par=ar;
  这里说明restar是访问由malloc()分配的内存的唯一且初始的方式。par就不是了。
   那么:
  for(n=0;n<10;n++)
  {
   par[n]+=5;
   restar[n]+=5;
   ar[n]*=2;
   par[n]+=3;
   restar[n]+=3;
  }
 因为restar是访问分配的内存的唯一且初始的方式,那么编译器可以将上述对restar的操作进行优化:
   restar[n]+=8; 
而par并不是访问数组ar的唯一方式,因此并不能进行下面的优化:
   par[n]+=8;
 因为在par[n]+=3前,ar[n]*=2进行了改变。使用了关键字restric,编译器就可以放心地进行优化了。


因为是c99才支持的,所以使用gcc编译时要加编译选项-std=c99


阅读(895) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~