what is this casting (long*)x when x is void**

30 Views Asked by At

So I found the following code in a program:

static void* InterlockedExchangePtr(void** x, void* switchval)
{
    return _InterlockedExchange((long*)(x), (long)(switchval));
}

(I know about atomics, only thing I don't quite get is what is this typecast (long*)(x))

1

There are 1 best solutions below

3
guard3 On

This casts x, which is a pointer to void pointer, to a pointer to long.

That cast is necessary because _InterlockedExchange expects a long* as its first argument and void** can't be implicitly converted to that.

https://learn.microsoft.com/en-us/cpp/intrinsics/interlockedexchange-intrinsic-functions