Sign in to follow this  
Cygon

Decimal string to Pointer via StringStream

Recommended Posts

C++ iostreams provide a method to output (void *) pointers. The pointer is written to and read from a stream in hexadecimal notation.

How can I use decimal notation without resorting to a reinterpret_cast and ugly #ifdefs?

I tried this:
[CODE]
std::stringstream ss;

void *test = (void *)0x12345678;

// Output pointer as decimal number
ss << std::ios::dec << test; // Works!

// Read pointer from decimal number
void *x;
ss << dec; // Nope
ss.setf(std::ios::dec); // No change
ss >> dec >> x; // Neither
[/code]

All the input variants do nothing. The pointer will not be assigned and keeps its "help I'm uninitialized" value of 0xcccccccc. The fail bit of the stream is set - probably because it tried to _still_ parse the number in hex and it was too large.

Share this post


Link to post
Share on other sites
It might not answer your question, but [i]why[/i] do you want to (a) write the pointer out in decimal, and (2) why on Mars would you want to read in a pointer?

Share this post


Link to post
Share on other sites
Why are you trying to read a pointer from a stream? That seems incredibly dangerous.

Interestingly, the following code appears to compile on my system:
[code]
#include <iostream>


int main() {
void *foo;
if(std::cin >> foo) {
std::cout << "Read a pointer?\n";
} else {
std::cout << "Failed\n";
}
}
[/code]
However explicitly trying to call the operator fails:
[code]
#include <iostream>


int main() {
void *foo;
if(operator>>(std::cin, foo)) {
std::cout << "Read a pointer?\n";
} else {
std::cout << "Failed\n";
}
}
[/code]
[s]I suspect the former program is actually calling a built in operator >> after some type converting, for example converting the two sides to booleans or something.[/s] (See Alvaro's response, I am wrong).

Share this post


Link to post
Share on other sites
Calling `operator>>(std::cin, foo)' explicitly doesn't work if foo is an int either.

Reading a pointer from a stream is a valid operation, but there is no guarantee that the accepted format is decimal. In g++ the format seems to be hexadecimal. I can't bother to look it up in the standard, but my guess is that the only guarantee is that it will understand whatever format `<< foo' spits out.

Share this post


Link to post
Share on other sites
[quote]
Calling `operator>>(std::cin, foo)' explicitly doesn't work if foo is an int either.
[/quote]
Interesting, Needless to say, I don't explicitly call operators like that often. I was hoping an explicit call would allow me to "jump to declaration" in my IDE.

[quote]
Reading a pointer from a stream is a valid operation...
[/quote]
Also interesting. Yet another entry in the C++ minefield to watch out for. Edited by rip-off

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this