开发者

Appropriate cast for OSAtomicCompareAndSwapPtrBarrier

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 开发者_StackOverflow社区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?


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:

bool result = OSAtomicCompareAndSwapPtrBarrier(old, new, &(void*)cur);

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜