I'm dealing with some code that uses an external library in which you can pass values to callbacks via a
Unfortunately, the previous person working on this code decided to just pass integers to these callbacks by casting an integer to a void pointer (
I'm now working on cleaning up this mess, and I'm trying to determine the "proper" way to cast an integer to/from a
void*. Unfortunately, fixing the use of the void pointers is somewhat beyond the scope of the rework I'm able to do here.
Right now, I'm doing two casts to convert from/to a void pointer:
Since I'm on a 64 bit machine, casting directly (
(int)void_p) results in the error:
error: cast from 'void*' to 'int' loses precision [-fpermissive]
The original implementation did work with
-fpermissive, but I'm trying to get away from that for maintainability and bug-related issues, so I'm trying to do this "properly", e.g. c++ casts.
Casting directly to an int (
static_cast<int>(void_p)) fails (
error: invalid static_cast from type 'void*' to type 'int'). My understanding of
reinterpret_cast is that it basically just causes the compiler to treat the address of the value in question as the cast-to data-type without actually emitting any machine code, so casting an
int directly to a
void* would be a bad idea because the
void* is larger then the
int (4/8 bytes respectively).
I think using
intptr_t is the correct intermediate here, since it's guaranteed to be large enough to contain the integral value of the
void*, and once I have an integer value I can then truncate it without causing the compiler to complain.
Is this the correct, or even a sane approach given I'm stuck having to push data through a void pointer?