The most important property in this respect is the
Data Model of the platform in question. FreeBSD (amd64) as most of the Unix-like platforms uses LP64. That means, long and pointers are 64 bit, while short and int are 16 and 32 bit respectively. Win64 uses ILP64 though, and here also int is 64 bit.
I didn't want to go that deep into details, but, for the sake of completeness: There's a third 64bit data model (LLP64) where even
long is 32bit. It basically comes down to the fact, that ONLY the size of a pointer is dictated by the hardware architecture, everything else depends on the implementation.
If the code in question places the content of a pointer into an int variable, then that type of that variable should be changed to intptr_t.
That's probably the best you can do if you absolutely MUST store a pointer in an integer. On some "exotic" architectures,
intptr_t could actually be bigger than
void* because there is no integer matching the size of a pointer exactly. So, better avoid storing pointers in integers altogether.
Even converting between
void* and
void(*)void is
generally unsafe, they could have different sizes. Unfortunately,
dlsym() forces you to do this. The correct alternative
dlfunc() (see
dlopen(3)) isn't available on all platforms, notably Linux.
AFAIR, the guarantee given by
intptr_t is just being big enough to hold data pointers as well as function pointers without truncation.
For new programs it is best to utilize the explicit integer types from <stdint.h>.
Agreed, with one restriction:
IFF you need exact sized integers. Stick with the simple integer types where it doesn't matter when they're larger than the minimum required.
There's also a catch with using
<stdint.h> in C (not C++) programs: It's not available prior to
C99. This may sound ridiculous, but
MSVC doesn't support parts of
C99, including
<stdint.h>. So, when writing a C program using this and wanting to be portable to
MSVC, you have to do some preprocessor magic and define your own
u?int_\d\d_t for Win32 and Win64. BTDT.
I think after all these technical details and catches of the C language, it's quite obvious that subtle bugs related to integer and pointer sizes are quite numerous. I'd still say they SHOULD be easy to fix for the original authors (given some exceptions like CPU emulators written in C), but it can be really hard to spot them in third party code ....