- 论坛徽章:
- 0
|
原帖由 fantix 于 2006-9-28 17:24 发表
restrict 关键字只能用于指针。表明该指针是访问数据的唯一且初始的方式。
看个例子啊
int * restrict rptr = (int*) malloc(10*sizeof(int));
此时rptr是访问有malloc分配的内存的唯一且初始的方式。 ...
$ gcc -Wall -o test test.c
test.c: In function 'main':
test.c:5: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'rptr'
test.c:5: error: 'rptr' undeclared (first use in this function)
test.c:5: error: (Each undeclared identifier is reported only once
test.c:5: error: for each function it appears in.)
test.c:14: error: invalid operands to binary *
你的程序有错误。
其实,restrict的作用就是限制一个指针对一块内存的访问,进一步说就是如果一块内存区域通过一个受限制指针访问,那么它就不能通过另一个受限指针访问。引入restrict的目的是确保同一块内存上没有其它引用,让编译器更好地优化指令,生成更有效的汇编代码。 |
|