I'm doing some C++ work that requires me to convert "normal" (widely-used) datatypes such as int
to their fixed-size counterpoints such as std::int32_t
. I have some code that uses void*
, however, and this is causing me trouble.
I know the size of any given pointer is the same on a given system - Edit for future readers: it's been pointed out that there are no guarantees on the size, absolute or relative, of pointers.sizeof(void*) == sizeof(int*) == sizeof(short*) == sizeof(long*)
. But I also know the size of a pointer can vary from system to system (in particular, 32-bit vs 64-bit).
Is there a fixed-size type capable of holding a void*
? I think std::intptr_t
or std::uintptr_t
are potential candidates, but I'm not sure which (if either) is correct. This is mostly due to signedness; I'm not sure why there's a signed and an unsigned form of intptr_t
, since I wasn't under the impression a pointer's signedness could be controlled. Instinct says I'll want to use uintptr_t
since I don't believe pointers can be signed, but then what's intptr_t
there for?
A bit more information about my need: I'm working on a DLL and need to guarantee cross-compiler capability if possible. For integer-based types this isn't too bad; I'm able to convert them to std::intXX_t
types behind-the-scenes. But one of my DLL's functions does some raw memory manipulations and needs to return a void*
. I'm concerned about this causing me all sorts of trouble if I can't properly wrap it, but I don't know what to wrap it with: I've not been able to find any sort of guarantee about the size or data range of a pointer. I know for a fact that the memory itself is accessible outside the DLL; this isn't a problem. But making sure a void*
can get from the host EXE to my DLL, or vice versa, safely is a much bigger issue.