tags:

views:

379

answers:

6

I need to represent pointers as strings to the user. Sometimes the values might be saved to a file and transferred to a computer with different architecture (32 vs 64 bit is the main issue currently) and loaded from text file to be compared - I'm only going to compare loaded values with each other, but I'd still prefer to compare numbers than strings.

I'm currently using:

SomeClass* p;
...
printf("%ld", (uintptr_t)p);

but I wonder if this is portable (Windows and Linux are only important at this stage though), and whether this would break once 128-bit systems show up?

Edit: unless I decide to use uint64_t, and decide that 64bit is the rooftop, this cannot be done because some 64bit pointer might be outside 32bit integer range. So, I decided that it would be safer to compare strings even if it's slower.

+20  A: 

For pointers, always use %p---it's a format specifier specially designed for printing pointers in the right format. :-)

Chris Jester-Young
But it's not portable. At least, it doesn't given consistent output between platforms. In visual studio, hex values comes out in caps. In gcc, lowercase.
*shakes head* Is that difference even worth bothering about? You can _never_ have addresses appear the same way across all platforms---e.g., in 16-bit real-mode DOS programs, addresses are usually printed in xxxx:yyyy format. (You can't remove the colon "just to be consistent between platforms"---that would be very misleading, because the address space in real mode DOS programs is not flat.)
Chris Jester-Young
e.g., 0x11110000 and 0x11120000, in flat addressing model, is 0x10000 bytes apart. However, in 16-bit real-mode DOS programs, 1111:0000 and 1112:0000 are 16 bytes apart.
Chris Jester-Young
@Chris: This is the main reason I wrote this question. Using uintptr_t seems to ensure that format of pointer string IS the same across platforms, because it's plain number.
Milan Babuškov
as you said you wanted this to represent it to the user. i wonder what good is it to represent them nonsensical numbers. why not keep it in a format they understand
Johannes Schaub - litb
+6  A: 

printf has a %p formatter which I believe is standardised:

printf( "%p", p );

but as you are using C++, ostreams already overload pointer output:

#include <iostream>
using namespace std;;

class A {};

int main() {
    A a;
    cout << &a << endl;
}

produces:

0x22ff6f
anon
Very, very standardised. :-P
Chris Jester-Young
Be carefull though. As they also specialise the output operator for char* and print a C-String.
Martin York
+9  A: 

I'd do this:

std::cout << p;

If you have your heart set on cstdio:

printf("%p", p);
Fred Larson
Thanks Fred. Actually, I'm using wxWidgets which has wxString::Format function that uses snprintf internally so that's why I needed printf example.
Milan Babuškov
+3  A: 

Are you looking for the %p formatting string?

printf("%p", p);

This'll give the hex-encoded address the pointer points to (and, I think, formats NULLs for you as well).

eduffy
A: 

I would have done:

cout << p << endl;

Strings (of the same encoding) are portable. If you want to compare them as pointers, you have to parse the string back into an unsigned long first.

As for 128-bit systems, they're a ways off. WIth 64 bits, you can directly address 16.8 million terabytes of RAM.

Paul J. Lucas
A: 

It can also be done as

printf("%#x", p);

This would ensure similar format (0x...) across platforms.