Pointer question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BCC

    Pointer question

    Hi,

    If I have something like this:
    CMyClass* p1;
    CMyClass* p2;

    I know I can do:
    p1 = new CMyClass();
    p2 = p1;

    Now both my pointers point to the same object. Cool so far.

    But is it possible to do this -before- you initialize your pointers?

    p2 = p1;
    p1 = new CMyClass();

    and have both pointers point to the same object?

    I don't think so, but thought I'd check for sure.

    Thanks,
    Bryan


  • David White

    #2
    Re: Pointer question

    BCC <a@b.c> wrote in message
    news:wI3Ta.2868 $WP6.99576061@n ewssvr21.news.p rodigy.com...[color=blue]
    > Hi,
    >
    > If I have something like this:
    > CMyClass* p1;
    > CMyClass* p2;
    >
    > I know I can do:
    > p1 = new CMyClass();
    > p2 = p1;
    >
    > Now both my pointers point to the same object. Cool so far.
    >
    > But is it possible to do this -before- you initialize your pointers?
    >
    > p2 = p1;
    > p1 = new CMyClass();
    >
    > and have both pointers point to the same object?[/color]

    Unless your computer runs on tachyons, then no.

    If p1 has not been initialized before the first line, then there is an
    extremely remote possibility that it will accidentally be the same address
    as that returned by the "new" expression.

    DW



    Comment

    • Josephine Schafer

      #3
      Re: Pointer question


      "BCC" <a@b.c> wrote in message
      news:wI3Ta.2868 $WP6.99576061@n ewssvr21.news.p rodigy.com...[color=blue]
      > Hi,
      >
      > If I have something like this:
      > CMyClass* p1;
      > CMyClass* p2;
      >
      > I know I can do:
      > p1 = new CMyClass();[/color]

      No need of the braces.
      [color=blue]
      > p2 = p1;
      >
      > Now both my pointers point to the same object. Cool so far.
      >
      > But is it possible to do this -before- you initialize your pointers?[/color]

      No.[color=blue]
      >
      > p2 = p1;[/color]

      Copy the junk address p1 is holding into p2.
      [color=blue]
      > p1 = new CMyClass();[/color]

      p1 now holds a valid heap address while p2 still holds the junk address.[color=blue]
      >
      > and have both pointers point to the same object?[/color]

      You see the difference now?

      --
      With best wishes,
      J.Schafer



      Comment

      • David White

        #4
        Re: Pointer question

        David White <no@email.provi ded> wrote in message
        news:w54Ta.1062 $sI.53877@nasal .pacific.net.au ...[color=blue]
        > BCC <a@b.c> wrote in message
        > If p1 has not been initialized before the first line, then there is an
        > extremely remote possibility that it will accidentally be the same address
        > as that returned by the "new" expression.[/color]

        Or this might do it on some compilers:
        p1 = new CMyClass;
        delete p1;
        // now your code
        p2 = p1;
        p1 = new CMyClass();

        DW



        Comment

        • Jakob Bieling

          #5
          Re: Pointer question

          "David White" <no@email.provi ded> wrote in message
          news:Qf4Ta.1063 $sI.53757@nasal .pacific.net.au ...[color=blue]
          > David White <no@email.provi ded> wrote in message
          > news:w54Ta.1062 $sI.53877@nasal .pacific.net.au ...[color=green]
          > > BCC <a@b.c> wrote in message
          > > If p1 has not been initialized before the first line, then there is an
          > > extremely remote possibility that it will accidentally be the same[/color][/color]
          address[color=blue][color=green]
          > > as that returned by the "new" expression.[/color]
          >
          > Or this might do it on some compilers:
          > p1 = new CMyClass;
          > delete p1;
          > // now your code
          > p2 = p1;
          > p1 = new CMyClass();[/color]


          Oi, and now you expect p1 to point to the same object as p2? Now the
          possibility that this works is jus as remote as to assume that an
          uninitialized p1 'accidently' points to p2. Actually, this code is even
          invalid (as I have learned a few days ago), because you are assigning p1 to
          p2.

          regards
          --
          jb

          (replace y with x if you want to reply by e-mail)


          Comment

          • Jakob Bieling

            #6
            Re: Pointer question

            "BCC" <a@b.c> wrote in message
            news:wI3Ta.2868 $WP6.99576061@n ewssvr21.news.p rodigy.com...[color=blue]
            > Hi,
            >
            > If I have something like this:
            > CMyClass* p1;
            > CMyClass* p2;
            >
            > I know I can do:
            > p1 = new CMyClass();
            > p2 = p1;
            >
            > Now both my pointers point to the same object. Cool so far.
            >
            > But is it possible to do this -before- you initialize your pointers?
            >
            > p2 = p1;
            > p1 = new CMyClass();
            >
            > and have both pointers point to the same object?
            >
            > I don't think so, but thought I'd check for sure.[/color]


            As you have it, no, like Josephine and David pointed out already. But
            you can change the code a little, so that p1 always points to the same thing
            p2 points to:

            CMyClass* p2;
            CMyClass*& p1 = p2; // make p1 a reference to a pointer

            p2 = new CMyClass ();
            // now p1 and p2 point to the same object

            hth
            --
            jb

            (replace y with x if you want to reply by e-mail)


            Comment

            • John Carson

              #7
              Re: Pointer question

              "BCC" <a@b.c> wrote in message
              news:wI3Ta.2868 $WP6.99576061@n ewssvr21.news.p rodigy.com[color=blue]
              > Hi,
              >
              > If I have something like this:
              > CMyClass* p1;
              > CMyClass* p2;
              >
              > I know I can do:
              > p1 = new CMyClass();
              > p2 = p1;
              >
              > Now both my pointers point to the same object. Cool so far.
              >
              > But is it possible to do this -before- you initialize your pointers?
              >
              > p2 = p1;
              > p1 = new CMyClass();
              >
              > and have both pointers point to the same object?
              >
              > I don't think so, but thought I'd check for sure.
              >
              > Thanks,
              > Bryan[/color]

              You need to use a reference, which is a kind of alias.

              CMyClass* p1;

              // makes p2 a reference to a pointer and initialises it to
              // be an reference to the pointer p1
              CMyClass* &p2 = p1;

              p1 = new CMyClass();


              --
              John Carson
              1. To reply to email address, remove donald
              2. Don't reply to email address (post here instead)

              Comment

              • John Ericson

                #8
                Re: Pointer question

                "Josephine Schafer" <jsf@usa.net> wrote in message
                news:bfiitc$f9s be$1@ID-192448.news.uni-berlin.de...[color=blue]
                >
                > "BCC" <a@b.c> wrote in message
                > news:wI3Ta.2868 $WP6.99576061@n ewssvr21.news.p rodigy.com...[color=green]
                > > Hi,
                > >
                > > If I have something like this:
                > > CMyClass* p1;
                > > CMyClass* p2;
                > >
                > > I know I can do:
                > > p1 = new CMyClass();[/color]
                >
                > No need of the braces.
                >[/color]

                Perhaps the OP is initializing a POD? ;)


                Comment

                • Default User

                  #9
                  Re: Pointer question



                  Jakob Bieling wrote:
                  [color=blue]
                  > Oi, and now you expect p1 to point to the same object as p2? Now the
                  > possibility that this works is jus as remote as to assume that an
                  > uninitialized p1 'accidently' points to p2. Actually, this code is even
                  > invalid (as I have learned a few days ago), because you are assigning p1 to
                  > p2.[/color]


                  It's not THAT ridiculous. Some people don't understand that
                  initializations store values, not operations. I've seen questions that
                  indicate people thought:


                  int a;
                  int b;
                  int c = a + b;


                  Would always set c to be the value of a + b. So if a or b changed, c
                  would automagically do so as well. It's not the way things work of
                  course.




                  Brian Rodenborn

                  Comment

                  • David White

                    #10
                    Re: Pointer question

                    Jakob Bieling <netsurf@gmy.ne t> wrote in message
                    news:bfj1hh$v4u $03$1@news.t-online.com...[color=blue]
                    > "David White" <no@email.provi ded> wrote in message
                    > news:Qf4Ta.1063 $sI.53757@nasal .pacific.net.au ...[color=green]
                    > > David White <no@email.provi ded> wrote in message
                    > > news:w54Ta.1062 $sI.53877@nasal .pacific.net.au ...[color=darkred]
                    > > > BCC <a@b.c> wrote in message
                    > > > If p1 has not been initialized before the first line, then there is an
                    > > > extremely remote possibility that it will accidentally be the same[/color][/color]
                    > address[color=green][color=darkred]
                    > > > as that returned by the "new" expression.[/color]
                    > >
                    > > Or this might do it on some compilers:
                    > > p1 = new CMyClass;
                    > > delete p1;
                    > > // now your code
                    > > p2 = p1;
                    > > p1 = new CMyClass();[/color]
                    >
                    >
                    > Oi, and now you expect p1 to point to the same object as p2?[/color]

                    No, I said, "Or this might do it on some compilers", which establishes the
                    entire post as implementation-dependent. All you need is for the second
                    "new" to return the same address as the first, which it might well do
                    because the first has been deleted. Regarding the "p2 = p1; ", I don't know
                    if it's invalid according to the standard, but, let's face it, is there a
                    compiler on Earth that wouldn't simply take the bit pattern in p1 and stick
                    it in p2? (again, I said "some compilers").
                    [color=blue]
                    > Now the
                    > possibility that this works is jus as remote as to assume that an
                    > uninitialized p1 'accidently' points to p2.[/color]

                    I don't think so.

                    DW



                    Comment

                    Working...