Some libraries require a callback function takes an void *
(not _Atomic void *
). Is it OK to cast an _Atomic Type *
to void *
and cast back to use it? Like the following code:
#include <stdio.h>
int Call(int(*f)(void*), void *arg){
return f(arg);
}
int Use(void *arg){
return *(_Atomic int *)arg;
}
_Atomic int g_x;
int main(int argc, const char **argv){
g_x = argc;
int y = Call(Use, (void*)&g_x);
printf("%d\n",y);
}
C 2018 6.3.2.3 1 says:
A pointer to
void
may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer tovoid
and back again; the result shall compare equal to the original pointer.
So there is no issue in merely converting the pointer to void *
and back; the behavior is defined by the C standard. This specification of the conversions does not restrict them regarding atomic types in any way.
The properties regarding atomic types apply to accessing atomic objects. (In particular, C 2018 6.7.2.4 4 says “The properties associated with atomic types are meaningful only for expressions that are lvalues…”) As long as the void *
is not used to access the object through some non-atomic type, it has no consequence on the use of the object.