How to store a memory address in an integer?

13,155

Solution 1

intptr_t and uintptr_t are integer types that are large enough to hold a void*. They are defined by C++11 in <cstdint> and by C99 in <stdint.h>

If uintptr_t is not available, you could try uintmax_t, defined in the same header(s) or by Boost in <boost/cstdint.hpp>.

Solution 2

One way to convert the address of an object to an integral type in C++ (without resorting to C-style casts) is to use a reinterpret_cast. An integral type guaranteed to be able to hold a pointer address as mentioned in the other answers is uintptr_t.

uintptr_t myint = reinterpret_cast<uintptr_t>(&myobject)

or

uintptr_t myint = reinterpret_cast<uintptr_t>(mypointer)

Share:
13,155
Cookie
Author by

Cookie

Updated on June 21, 2022

Comments

  • Cookie
    Cookie about 2 years

    This is exactly the same question title as found here - I would also like to store a memory address in a variable - or rather, a void* in a variable. However, I'd rather store it in some form of an int rather than a string, as I'd like to cast it back to a pointer afterwards.

    This is because it is a member of a class that I would then like to serialize with boost serialize, and if I did use a void*, boost serialize might try to store what the pointer is pointing to, which wouldn't be very sensible in my case.

    I need this for 32 and 64 bit gcc and MSVC, so basically I was wondering whether there was an inbuilt integer type that was the size of pointers on the same platform. Alternatively, I guess I would need to IFDEF my own type?

    • Jonas Schäfer
      Jonas Schäfer over 11 years
      If it's for serialization, storing a pointer doesn't seem sensible to me at all. Just curious, what does it point to?
    • Cookie
      Cookie over 11 years
      A complex environment - while serializing, it is a full pointer to said environment. When de-serializing, it merely serves as identifier to know whether two objects were referring to the same environment or not. Apart from that, there are plenty of reasons to serialize pointers, but usually only with the pointed to objects - there are plenty of example in boost serialization of such cases, but just one would be shared pointers, where the pointed to object then only needs to be serialized once.
    • Yakk - Adam Nevraumont
      Yakk - Adam Nevraumont over 11 years
      I know this technique. It is not worth it. Do not turn your serialized int_ptr back into a pointer. Your code will end up dereferencing that not a pointer. Only store real working pointers in pointer variables. If you must create a parallel structure with an int_ptr in place of the pointer, but at the moment you want to make the real struct, have some real data to point to. I get it, your solution seems so easy... but it isn't.
    • Cookie
      Cookie over 11 years
      That is actually not the technique we are using here. We literally convert it only back to output it, very much like std::cout << reinterpret_cast<void*>(xxx) << ...
  • Cookie
    Cookie over 11 years
    You wouldn't put the data back - the only reason I cast it back to a pointer is so that it has the same format as before when I output it to an ostream.
  • Uyghur Lives Matter
    Uyghur Lives Matter over 10 years
    It may be helpful to explain what your statement is doing. Also if you already have mypointer why take the address a second time?
  • Riot
    Riot over 10 years
    You're right, it's not totally self-explanatory. Brief explanation added.