“易失性限制”指针有实际用途吗?
我可以看到 const volatile 限定变量的实际用途,就像
const volatile uint64_t seconds_since_1970;
底层硬件机制每秒更新该值一样,但该变量在(可能是嵌入式)硬件中不可写入。 由于所有三个(C11 中为四个)类型限定符都被认为是独立的,因此所有组合似乎都是允许的。但我无法想象现实生活中的情况,其中 restrict 易失性
限定指针确实有意义:
uint32_t * restrict volatile pointer_to_some_uint32;
[编辑:澄清:易失性
和 limit
适用于指针,而不是指向的对象!]
这是语言允许的构造,但其本身无用,还是我错过了一些可能有价值的应用程序区域?
I can see practical use for a const volatile
qualified variable, like
const volatile uint64_t seconds_since_1970;
if an underlying hardware mechanism updates the value every second, but the variable is not writable in the (possibly embedded) hardware.
And since all three (four in C11) type qualifiers are considered independent, all combinations do seem to be allowed. But I'm at a loss imagining a real-life situation where a restrict volatile
qualified pointer would really make sense:
uint32_t * restrict volatile pointer_to_some_uint32;
[EDIT: To clarify: Both volatile
and restrict
apply to the pointer, not to the object pointed to!]
Is this a construct allowed by the language but useless by itself, or am I missing some application area where this can be valuable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有限制,非易失性指针可能会成为易失性指针的别名。因此,每次通过易失性指针修改对象后,必须丢弃所有潜在指针引用的同一类型对象的寄存器缓存值。
使用restrict,您可以告诉编译器,易失性指针不会别名,因此易失性的开销仅适用于指向的对象,而不是可通过指针访问的所有其他相同类型的对象。
Without restrict, a non-volatile pointer could alias a volatile pointer. Thus, after every modification of an object through the volatile pointer, register-cached values of all potentially-pointer-referenced objects of the same type must be discarded.
With restrict, you can tell the compiler the volatile pointer will not alias, so that the overhead of volatile only applies to the object pointed to, and not all other objects of the same type that might be accessible via pointers.