I often see (in Linux kernel for example) that unsigned long
is used to hold the pointers. I wonder what is the reason for this given that size of a pointer may be larger than integer type (including long).
Is it portable to keep a pointer in unsigned long
rather than in uintptr_t
in Linux user space applications? (Although I know that uintptr_t
guarantees to convert from void *
to uintptr
integer and back without loss of information)
Thanks.
"Yes" in the sense that it will work on any current port of Linux and quite likely in the future. But why? There is a perfectly good typedef that specifies the intent too:
uintptr_t
- and it makes your code portable to Win64 too.uintptr_t
was a C99 invention and Linux predates C99 by years. Back then there was no convention for specifying an integer wide enough to hold a pointer - but then, there was nounsigned long long
either, except by compiler extension, sounsigned long
was all that you could reasonably expect to hold a pointer, if anything did, and so it was. By now, it is rather that for any new architecture that runs Linux need to pick type forlong
so that it is wide enough for pointersize_t
etc.When 64-bit Windows came, too many things were relying on some representation for
unsigned long
, and it remained as 32 bit instead of the type required to hold pointer. To my knowledge of all relevant platforms today only on Win64unsigned long
is not wide enough to hold a pointer.