0

I'm trying to store the address of a pointer as a string. In other words, I want to insert the content of the bytes that make up the address into a char vector.

What is the best way of doing this?

I need a fully portable method, including for 64 bit system.

Subway
  • 5,286
  • 11
  • 48
  • 59
  • adding every character i.e address into vector? – Dineshkumar Jun 02 '13 at 14:54
  • 1
    See http://stackoverflow.com/questions/7850125/convert-this-pointer-to-string. – Mihai8 Jun 02 '13 at 14:56
  • @Dineshkumar, is it clearer now? – Subway Jun 02 '13 at 14:58
  • 1
    If the pointer's value (i.e. the object's address) is "001AFA8C" - what would the desired output look like? A vector of chars can either contain the single characters of the hexadecimal representation ("8" and "C" etc) or the actual bytes as decimal numbers ("8C" as "140" etc). – Niko Jun 02 '13 at 15:35
  • @Niko, I haven't thought of the first option. Actually... both fit my needs. – Subway Jun 02 '13 at 15:46

5 Answers5

2

The simplest way is to do

char buf[sizeof(void*) * 2 + 3];
snprintf(buf, sizeof(buf), "%p", /* the address here */ );
Guillaume
  • 2,044
  • 12
  • 11
  • Thanks for answering. Why 32? isn't it way bigger than needed? – Subway Jun 02 '13 at 15:00
  • @Subway it's too much but it will work on any platforms right now. I am editing with a portable way. – Guillaume Jun 02 '13 at 15:02
  • 32-bit uses 8+1, 64-bit uses 16+1, so this method allows up to 128-bit, but it won't have a null-cap in that case. You could size the buffer according to the platform, e.g. `char buf[sizeof(size_t)*2+1]`. – Dave Jun 02 '13 at 15:02
  • @Guillaume it does? hmm, my usual reference site has failed me. – Dave Jun 02 '13 at 15:07
  • In that case I suggest this alternative format string: `snprintf( buf, sizeof(buf), "%*zx", sizeof(size_t) * 2, (size_t) myPointerHere );` which will not print the pointless `0x`. – Dave Jun 02 '13 at 15:13
  • @Dave I guess that depends if you want it or not. If you want to get rid of it, I'd use "%*tx" because size_t does not guarantee that it will be big enough (even if it will work in practice) – Guillaume Jun 02 '13 at 15:16
2

To get an array (or vector, if you prefer that) of the actual bytes of the address, this should do the trick:

int foo = 10;
int* bar = &foo;

// Interpret pointer as array of bytes
unsigned char const* b = reinterpret_cast<unsigned char const*>(&bar);

// Copy that array into a std::array
std::array<unsigned char, sizeof(void*)> bytes;
std::copy(b, b + sizeof(void*), bytes.begin());

To get an array containing the hexadecimal representation split up into single characters (whatever sense that makes), I'd use a stringstream - as some of the others already suggested. You can also use snprintf to get a string representation of the address, but that's more the C-style way.

// Turn pointer into string
std::stringstream ss;
ss << bar;
std::string s = ss.str();

// Copy character-wise into a std::array (1 byte = 2 characters)
std::array<char, sizeof(void*) * 2> hex;
std::copy(s.begin(), s.end(), hex.begin());
Niko
  • 26,516
  • 9
  • 93
  • 110
1
std::string serialized (std::to_string ((intptr_t) ptr));
ArtemGr
  • 11,684
  • 3
  • 52
  • 85
  • Thanks Artem. Isn't there a preferable c++ casting than the c-style casting? – Subway Jun 02 '13 at 15:13
  • Subway, This is a matter of taste. Sometimes a less verbose version is easier to read. If you're a fan of different guidelines, feel free to use them. – ArtemGr Jun 02 '13 at 15:22
  • Subway, intptr_t is a C style for that matter (from `stdint.h`). You can use `std::intptr_t` if you so wish. – ArtemGr Jun 02 '13 at 15:39
  • thanks. I wan't asking about using std::intptr_t, but about using a c++ casting, like `reinterpret_cast` or one of the others. Could you please tell me which one fits this case? – Subway Jun 02 '13 at 15:42
1

C++ way to dos this would be to use string streams

#include <string>
#include <sstream>

int main()
{
    MyType object;
    std::stringstream ss;
    std::string result;

    ss << &object; // puts the formatted address of object into the stream
    result = ss.str(); // gets the stream as a std::string

    return 0;

}
0
void storeAddr(vector<string>& v,void *ptr)
{
    stringstream s;
    s << (void*)ptr ;
    v.push_back(s.str());
}
irappa
  • 749
  • 4
  • 11