I am trying to adapt an existing code to a 64 bit machine. The main problem is that in one function, the previous coder uses a void* argument that is converted into suitable type in the function itself. A short example:
void function(MESSAGE_ID id, void* param)
{
if(id == FOO) {
int real_param = (int)param;
// ...
}
}
Of course, on a 64 bit machine, I get the error:
error: cast from 'void*' to 'int' loses precision
I would like to correct this so that it still works on a 32 bit machine and as cleanly as possible. Any idea ?
Use
intptr_t
anduintptr_t
.To ensure it is defined in a portable way, you can use code like this:
Just place that in some .h file and include wherever you need it.
Alternatively, you can download Microsoft’s version of the
stdint.h
file from here or use a portable one from here.'size_t' and 'ptrdiff_t' are required to match your architecture (whatever it is). Therefore, I think rather than using 'int', you should be able to use 'size_t', which on a 64 bit system should be a 64 bit type.
This discussion unsigned int vs size_t goes into a bit more detail.
The best thing to do is to avoid converting from pointer type to non-pointer types. However, this is clearly not possible in your case.
As everyone said, the uintptr_t is what you should use.
This link has good info about converting to 64-bit code.
There is also a good discussion of this on comp.std.c
Several answers have pointed at
uintptr_t
and#include <stdint.h>
as 'the' solution. That is, I suggest, part of the answer, but not the whole answer. You also need to look at where the function is called with the message ID of FOO.Consider this code and compilation:
You will observe that there is a problem at the calling location (in
main()
) — converting an integer to a pointer without a cast. You are going to need to analyze yourfunction()
in all its usages to see how values are passed to it. The code inside myfunction()
would work if the calls were written:Since yours are probably written differently, you need to review the points where the function is called to ensure that it makes sense to use the value as shown. Don't forget, you may be finding a latent bug.
Also, if you are going to format the value of the
void *
parameter (as converted), look carefully at the<inttypes.h>
header (instead ofstdint.h
—inttypes.h
provides the services ofstdint.h
, which is unusual, but the C99 standard says [t]he header<inttypes.h>
includes the header<stdint.h>
and extends it with additional facilities provided by hosted implementations) and use the PRIxxx macros in your format strings.Also, my comments are strictly applicable to C rather than C++, but your code is in the subset of C++ that is portable between C and C++. The chances are fair to good that my comments apply.