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. Advertising

  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. Advertising

  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. Advertising

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:
    7,334
    avnrao
    May 4, 2004
  2. Patrice
    Replies:
    0
    Views:
    923
    Patrice
    May 4, 2004
  3. Raterus
    Replies:
    0
    Views:
    463
    Raterus
    Jun 2, 2004
  4. Alex Stevens
    Replies:
    0
    Views:
    805
    Alex Stevens
    Aug 10, 2004
  5. candide
    Replies:
    65
    Views:
    1,449
Loading...

Share This Page