470,849 Members | 1,144 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

Question: how to read an address?

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
Jun 27 '08 #1
3 1144
shuisheng wrote:
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?
--
Christian Hackl
Jun 27 '08 #2
On Jun 6, 10:47 am, Christian Hackl <ha...@sbox.tugraz.atwrote:
shuisheng wrote:
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?

--
Christian Hackl
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!
Jun 27 '08 #3
shuisheng wrote:
On Jun 6, 10:47 am, Christian Hackl <ha...@sbox.tugraz.atwrote:
>But what are you trying to accomplish, anyway? Why don't you put the int
in the stream rather than its address?

What I am doing is coding a smart pointer.
Is this some kind of exercise? If not, you should have a look at the
smart pointer classes offered by Boost:

http://www.boost.org/doc/libs/1_35_0.../smart_ptr.htm
--
Christian Hackl
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Jake D | last post: by
1 post views Thread by kazack | last post: by
8 posts views Thread by Chad Cartwright | last post: by
3 posts views Thread by reageer | last post: by
39 posts views Thread by anonymous | last post: by
21 posts views Thread by Chad | last post: by
1 post views Thread by Keaven Pineau | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.