C 语言 restrict 关键字的使用
来源:互联网 发布:java类和对象代码 编辑:程序博客网 时间:2024/05/22 05:06
C99中新增加了restrict修饰的指针:
由restrict修饰的指针是最初唯一对指针所指向的对象进行存取的方法,
仅当第二个指针基于第一个时,才能对对象进行存取。
对对象的存取都限定于基于由restrict修饰的指针表达式中。
由restrict修饰的指针主要用于函数形参,或指向由malloc()分配的内存空间。
restrict数据类型不改变程序的语义。
编译器能通过作出restrict修饰的指针是存取对象的唯一方法的假设,更好地优化某些类型的例程。
restrict是c99标准引入的,它只可以用于限定和约束指针,
并表明指针是访问一个数据对象的唯一且初始的方式.
即它告诉编译器,所有修改该指针所指向内存中内容的操作都必须通过该指针来修改,
而不能通过其它途径(其它变量或指针)来修改;这样做的好处是,
能帮助编译器进行更好的优化代码,生成更有效率的汇编代码.如
int *restrict ptr,
ptr 指向的内存单元只能被 ptr 访问到,任何同样指向这个内存单元的其他指针都是未定义的,
直白点就是无效指针。
restrict 的出现是因为 C 语言本身固有的缺陷,
C 程序员应当主动地规避这个缺陷,而编译器也会很配合地优化你的代码.
例子 :
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;}
一个是编译器,它告诉编译器可以自由地做一些有关优化的假定。
另一个读者是用户,他告诉用户仅使用满足restrict要求的参数。
一般,编译器无法检查您是否遵循了这一限制,如果您蔑视它也就是在让自己冒险。
To help the compiler determine memory dependencies,
you can qualify a pointer, reference, or array
with the restrict keyword.
The restrict keyword is a type qualifier that may be
applied to pointers, references, and arrays.
Its use represents a guarantee by the programmer
that within the scope of the pointer declaration
the object pointed to can be accessed only by that pointer.
Any violation of this guarantee renders the program undefined.
This practice helps the compiler optimize certain sections of code
because aliasing information can be more easily determined.
Use of the restrict type qualifier with pointers
void func1(int * restrict a, int * restrict b){ /* func1's code here */}
In the example that follows, the restrict keyword is
used to tell the compiler that the function func1 is
never called with the pointers a and b pointing
to objects that overlap in memory.
You are promising that accesses through a and b
will never conflict; this means that a write through one pointer
cannot affect a read from any other pointer.
The precise semantics of the restrict keyword are
described in the 1999 version of the ISO C standard.
Use of the restrict type qualifier with arrays
void func2(int c[restrict], int d[restrict]){ int i; for(i = 0; i < 64; i++) { c[i] += d[i]; d[i] += 1; }}
This example illustrates using the restrict keyword when passing arrays to a function.
Here, the arrays c and d should not overlap, nor should c and d point to the same array.
» 下一篇:回车(carriage return : \r) 换行(line feed : \n)
- C语言restrict关键字的使用
- C 语言 restrict 关键字的使用
- C 语言restrict 关键字的使用浅谈
- C 语言restrict 关键字的使用浅谈
- C 语言restrict 关键字的使用浅谈
- C 语言restrict 关键字
- C语言关键字 restrict
- C 语言restrict 关键字
- c语言中restrict关键字的理解
- C语言中restrict关键字
- C语言关键字 restrict 分析
- C语言的 restrict
- C语言中关键字restrict的用法(linux 为 __restrict)
- C关键字restrict的理解
- C语言中restrict关键字学习
- C语言中 restrict 关键字学习
- C语言中restrict关键字学习
- C语言中restrict关键字学习
- ubuntu配置jdk脚本以及导致开不了机的解决方案
- 回车(carriage return : \r) 换行(line feed : \n)
- gcc对C语言的扩展:函数参数构造
- U盘强制格式化的解决方案
- redhat5.4关掉防火墙
- C 语言 restrict 关键字的使用
- NavigationController的使用及下一级页面标题的设置
- 如何更改linux文件的拥有者及用户组(chown和chgrp)
- asp.net向网页输出文件及flex发送HttpService的总结(个人笔记)
- 构造函数抛出异常
- 利用MySQL加密函数保护Web网站敏感数据
- struts2源码研究
- windows下编译ffmpeg+segmenter(HLS)
- 带会整理