Re: error C2664 with InterlockedExchangePointer

Tech-Archive recommends: Fix windows errors by optimizing your registry



Doug Harrison [MVP] wrote:
> On Thu, 29 Sep 2005 09:53:27 +0200, Ulrich Eckhardt
> <eckhardt@xxxxxxxxxxxxxx> wrote:
>>You can't convert between these two pointers, as they are unrelated. The
>>right thing to do is to to cast appropriately:
>>void** arg =
>>static_cast<void**>(static_cast<void*>(&mQ_waiting_for_write));
>>
>>Using reinterpret_cast will probably also work, although its effects are
>>implementation defined.
>
> The result of your two static_casts is equally unspecified.

Why?

> Practically speaking, there is no question whatsoever that:
>
> reinterpret_cast<void**>(&mQ_waiting_for_write)
>
> which is equivalent to:
>
> (void**) &mQ_waiting_for_write

The standard says that the results of reinterpret_cast are implementation
defined. The only guarantee given is that a reinterpret_cast back to the
initial type yields the same value, i.e. that it is reversible.

Other than that, consider the added safety that C++ style casts offer to C
style casts.

> will have the desired effect on a compiler for Windows. If it didn't, then
> pretty much all COM code would break. For that matter, I don't know of any
> system for which it won't work as desired.

A COM implementation might depend on compiler support anyway, so my intent
of portability is fooled anyway. Other than that, I have never seen a
system where a pointer is not 32 bits, but I know other systems are out
there and I don't take such chances.

Uli


.



Relevant Pages

  • Re: regarding free function in c library
    ... type void* and when we invoke them with pointers to any type, ... No, C does not support "implicit casts", no such thing exists in the ...
    (comp.lang.c)
  • Re: Borland Delphi + 64Bits
    ... If you have code that casts between Pointer and Integer, ... > pointers), but should be a little easier to port to 64 bit. ... I think the porting pain between .NET and Win64 will balance out. ... the compiler will reject the questionable ...
    (borland.public.delphi.non-technical)
  • Re: void pointer arithmetic
    ... As for doing pointer arithmetic on the results: If all the pointers ... static int str_qcmp(const void *a, ... If casts are not no-ops, ... receives struct foo pointers, so it doesn't convert them from void* to ...
    (comp.lang.c)
  • Re: cast in memcpy()
    ... >> Your informant is wrong. ... No such casts are necessary in C. ... That only applies to pointers to objects, ... The latter cannot be converted to `void *' at all. ...
    (comp.lang.c)