C++中restrict修饰符作用

今天查看POSIX的API时候,发现里面有一行

int pthread_create(pthread_t * restrict thread,const pthread_attr_t *restrictattr,void *(*start_routine)(void*),void*restrict arg);
开始和不解为什么变量前面要加上restrict修饰符呢?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是一个限定符,这个关键字据说来源于古老的FORTRAN,主要用来修饰指针指向的内存不能被别的指针引用。

restrict同样可以用于数组的声明:

If you specify type qualifiers such as void foo(int * restrict a);, the C compiler expresses it with array syntax void foo(int a[restrict]); which is essentially the same as declaring a restricted pointer.

其实restrict同const或valiate一样是一个修饰符而已,告诉编译器被restrict修饰的指针所指 向的对象,只能通过这个指针或基于这个指针的其他指针进行操作,即限制访问用restrict限制的指针指向的对象只能通过这个指针访问,这对编译器的优 化很有好处。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值