Question: how to read an address?

Discussion in 'C++' started by shuisheng, Jun 6, 2008.

  1. shuisheng

    shuisheng Guest

    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
     
    shuisheng, Jun 6, 2008
    #1
    1. Advertisements

  2. 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
     
    Christian Hackl, Jun 6, 2008
    #2
    1. Advertisements

  3. shuisheng

    shuisheng Guest

    On Jun 6, 10:47 am, Christian Hackl <> wrote:
    > 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!
     
    shuisheng, Jun 6, 2008
    #3
  4. shuisheng wrote:

    > On Jun 6, 10:47 am, Christian Hackl <> wrote:
    >> 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/libs/smart_ptr/smart_ptr.htm


    --
    Christian Hackl
     
    Christian Hackl, Jun 6, 2008
    #4
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. avnrao
    Replies:
    1
    Views:
    8,026
    avnrao
    May 4, 2004
  2. Alex Stevens
    Replies:
    0
    Views:
    920
    Alex Stevens
    Aug 10, 2004
  3. =?Utf-8?B?QW5nZWw=?=

    Pass a Web Address and return a File System Address

    =?Utf-8?B?QW5nZWw=?=, Sep 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    710
    =?Utf-8?B?U3RldmUgQy4gT3Jy?=
    Sep 21, 2005
  4. SJ

    MAC Address/Physical Address?

    SJ, Jan 7, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    762
  5. Replies:
    2
    Views:
    801
    Christopher Reed
    Apr 15, 2006
  6. AbraKadabra
    Replies:
    2
    Views:
    740
    Gordon Beaton
    Sep 21, 2005
  7. Krishna Sagiraju

    convert virtual address to physical address

    Krishna Sagiraju, Jul 10, 2004, in forum: C Programming
    Replies:
    3
    Views:
    1,488
    Malcolm
    Jul 10, 2004
  8. candide
    Replies:
    65
    Views:
    1,803
Loading...