NO

Author Topic: Pointers as numeric identifiers  (Read 222 times)

Offline PaoloC13

  • Member
  • *
  • Posts: 44
Pointers as numeric identifiers
« 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?

Offline algernon_77

  • Member
  • *
  • Posts: 31
Re: Pointers as numeric identifiers
« Reply #1 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.

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2139
    • http://www.smorgasbordet.com
Re: Pointers as numeric identifiers
« Reply #2 on: July 01, 2021, 10:45:28 pm »
intptr_t (signed) or uintptr_t (unsigned) from <stdint.h> seems like the best choice.
/Pelle

Offline PaoloC13

  • Member
  • *
  • Posts: 44
Re: Pointers as numeric identifiers
« Reply #3 on: July 02, 2021, 12:46:07 am »
Yes, I will use uintptr_t
Thank you all

Offline algernon_77

  • Member
  • *
  • Posts: 31
Re: Pointers as numeric identifiers
« Reply #4 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  :) !