OSAtomicCompareAndSwapPtrBarrier 的适当转换
在此程序中:
int x, y;
int *old = &x;
int *new = &y;
int * volatile cur = &x;
OSAtomicCompareAndSwapPtrBarrier(old, new, &cur);
我收到此警告:
将“int *易失性*”传递给“void *易失性*”类型的参数时不兼容的指针
在 XCode 4.0.1 的默认编译器上, 。 (实际上是实时问题。)
现在,我知道通常我不能将 int** 转换为 void** 。但如果我不这样做,我就看不到任何方法可以在不收到上述警告的情况下比较和交换和 int* 。我应该忽略这个警告,假设它是指针大小不统一的时代的遗迹,还是我误解了什么?
In this program:
int x, y;
int *old = &x;
int *new = &y;
int * volatile cur = &x;
OSAtomicCompareAndSwapPtrBarrier(old, new, &cur);
I get this warning:
Incompatible pointer passing 'int *volatile *' to parameter of type 'void *volatile *'
on XCode 4.0.1's default compiler. (Live Issues, actually.)
Now, I know that in general I can't cast int** to void** . But if I don't, I don't see any way to compare-and-swap and int* without getting the above warning. Should I just ignore the warning, assuming it's a relic from a time of non-uniform pointer sizes, or am I misunderstanding something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为警告表明编译器认为从指针到指针到 int 到指针到指针到 void 的隐式转换是危险的。并不是说你不能进行这样的转换;而是说你不能进行这样的转换。该标准规定,只要满足类型对齐要求,指针就可以转换为指向不同类型的对象的指针。但有时,例如在使用严格别名的情况下,它可能会导致到问题。
我相信在你的情况下,警告可以被忽略,或者可以通过显式强制转换来消除:
另外,请确保你不要忘记检查返回值;忽略原子比较和交换可能不成功(由于并发修改)的事实很少是安全的。
I think the warning says that the compiler considers implicit conversion from pointer-to-pointer-to-int to pointer-to-pointer-to-void as dangerous. It's not as if you could not do such a conversion; the standard says a pointer can be converted to a pointer to an object of different type, as long as type alignment requirements are satisfied. But sometimes, e.g. in case of strict aliasing being used, it can potentially lead to problems.
I believe in your case the warning can be ignored, or possibly eliminated with explicit casts:
Also, make sure you do not forget to check the return value; ignoring the fact that atomic compare-and-swap may not succeed (due to concurrent modifications) is rarely safe.