Question: how to read an address?

S

shuisheng

Dear All,

I have a probably simple question but annoying me a lot.

For example:

std::stringstream ss;
int * p = new int(1);
ss << p; // Output the address
void * q;
ss >> q; // I want to input the address, but give a compilation error.
if (q != NULL)
...

Any way to read an address and see if it is valid?

Thanks a lot!

Shuisheng
 
C

Christian Hackl

shuisheng said:
std::stringstream ss;
int * p = new int(1);
ss << p; // Output the address
void * q;
ss >> q; // I want to input the address, but give a compilation error.
if (q != NULL)
...

What's the error? The following complete program compiles fine with GCC
4.1.2, VC7 and Comeau 4.3.10.1 Beta (online).

#include <sstream>
#include <iostream>

int main()
{
std::stringstream ss;
int * p = new int(1);
ss << p;
void * q;
ss >> q;
std::cout << q << "\n";

}

But what are you trying to accomplish, anyway? Why don't you put the int
in the stream rather than its address?
 
S

shuisheng

What's the error? The following complete program compiles fine with GCC
4.1.2, VC7 and Comeau 4.3.10.1 Beta (online).

#include <sstream>
#include <iostream>

int main()
{
std::stringstream ss;
int * p = new int(1);
ss << p;
void * q;
ss >> q;
std::cout << q << "\n";

}

But what are you trying to accomplish, anyway? Why don't you put the int
in the stream rather than its address?

You are right. I have a typo in my testing. I used "void q" rather
than "void* q".

What I am doing is coding a smart pointer. To output it, I output
address and value. In reading, if the address is NULL, it means the
pointer does not point a value.

Thank you so much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top