C语言的 restrict

  from: http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=/com.ibm.vacpp7a.doc/language/ref/clrc03restrict_type_qualifier.htm 1,加了restrict后gcc编译的时候需要加 -std=c99 2,加了restrict的指针,表示该指针是唯一访问该内存的方式。但是我写了一个程序将一个restrict类型的指针赋值给另外一个指针,编译器没有报错。why? 有时间再研究: The restrict Type Qualifier The restrict type qualifier may only be applied to a pointer. A pointer declaration that uses this type qualifier establishes a special association between the pointer and the object it accesses, making that pointer and expressions based on that pointer, the only ways to directly or indirectly access the value of that object. A pointer is the address of a location in memory. More than one pointer can access the same chunk of memory and modify it during the course of a program. The restrict type qualifier is an indication to the compiler that, if the memory addressed by the restrict-qualified pointer is modified, no other pointer will access that same memory. The compiler may choose to optimize code involving restrict-qualified pointers in a way that might otherwise result in incorrect behavior. It is the responsibility of the programmer to ensure that restrict-qualified pointers are used as they were intended to be used. Otherwise, undefined behavior may result. If a particular chunk of memory is not modified, it can be aliased through more than one restricted pointer. The following example shows restricted pointers as parameters of foo(), and how an unmodified object can be aliased through two restricted pointers. void foo(int n, int * restrict a, int * restrict b, int * restrict c) { int i; for (i = 0; i < n; i++) a[i] = b[i] + c[i]; } Assignments between restricted pointers are limited, and no distinction is made between a function call and an equivalent nested block. { int * restrict x; int * restrict y; x = y; // undefined { int * restrict x1 = x; // okay int * restrict y1 = y; // okay x = y1; // undefined } } In nested blocks containing restricted pointers, only assignments of restricted pointers from outer to inner blocks are allowed. The exception is when the block in which the restricted pointer is declared finishes execution. At that point in the program, the value of the restricted pointer can be carried out of the block in which it was declared. C++ supports the restrict keyword as a non-orthogonal language extension for compatibility with C99. The compiler consumes and ignores the keyword, but issues a diagnostic message for incorrect usage. It is non-orthogonal because an existing C++ program can use restrict as a variable name. The __restrict__ keyword is provided to avoid invading the user namespace. All Standard C++ and C++98 specifications that apply to cv-qualifiers apply to the restrict and __restrict__ keywords. These specifications include the rules governing qualification conversion and function overloading. For example, the __restrict__ qualifier affects type specifications in the same way as a cv-qualifier. int * __restrict__ p; // p is a restricted pointer to int int * __restrict__ * q; // q is a pointer to a restricted pointer to int void foo(float * __restrict__ a, float * __restrict__ b); // a and b point to different (non-overlapping) objects The __restrict__ qualifier also affects parameter type specifications for overloaded functions in the same way as a cv-qualifier, thereby affecting the overload set. void foo(int * __restrict__ * a) { printf("First/n"); } // candidate function 1. void foo(int * * b) { printf("Second/n"); } // candidate function 2. int main() { int a; int *b=&a; int *__restrict__ *c=&b; int * *d =&b; foo(c); foo(d); } Candidate function 1 has a more cv-qualified function parameter than the candidate function 2. If the calling function is foo(d), candidate function 2 is the better candidate function. But if the calling function is foo(c), only candidate function 1 can be called.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值