Pelles C forum

C language => Expert questions => Topic started by: PaoloC13 on July 01, 2021, 12:07:11 AM

Title: Pointers as numeric identifiers
Post by: PaoloC13 on July 01, 2021, 12:07:11 AM
Wanting to use pointers as unique numeric identifiers of single data objects, what kind of data type do you suggest to correctly contain the numeric value of a pointer?
Now I'm going to use size_t. Is size_t always the same size of a pointer?
Is there a more appropriate choice?
Title: Re: Pointers as numeric identifiers
Post by: algernon_77 on July 01, 2021, 07:38:15 AM
Hello,

Pointers are 32 bit on a x86 app and 64 bit on a x64 app, so I would use UINT_PTR data type to hold a pointer.
size_t is basically the type that sizeof operator returns and, as I remember, it used to be guaranteed to be at least 16 bits. I don't know what is its maximum size on modern compilers.
Title: Re: Pointers as numeric identifiers
Post by: Pelle on July 01, 2021, 10:45:28 PM
intptr_t (signed) or uintptr_t (unsigned) from <stdint.h> seems like the best choice.
Title: Re: Pointers as numeric identifiers
Post by: PaoloC13 on July 02, 2021, 12:46:07 AM
Yes, I will use uintptr_t
Thank you all
Title: Re: Pointers as numeric identifiers
Post by: algernon_77 on July 02, 2021, 10:53:41 AM
Definitely a better choice, since there's no need to include all MS galore just for UINT_PTR/INT_PTR types.
A big thanks from me, too  :) !