Comparing pointers

Discussion in 'C++' started by carlosp, Mar 12, 2009.

  1. carlosp

    carlosp Guest

    Hello, just starting to work on pointers. Nice, but there is a very
    antiintuitive thing that bothers me...

    I have a collection of objects (with no meaningful "==" operator on
    them). I am using pointers to move them, sort them, etc. I want to
    evaluate if two pointers point to the same object. However, using

    ptr1 == ptr2

    yields true even if they are different as reported in std!!! Here is a
    piece of my code, and the output.

    if(test(i)==test(i+i)){
    std::cerr << "pointer i "<< test(i) << ", pointer i+1 "<<
    test(i+1) << "\n";
    abort();
    }


    with the result corresponding to that part of the program

    pointer i 0x102280, pointer i+1 0x1022c8
    Abort trap


    Whats going on here?? Please illuminate me!
     
    carlosp, Mar 12, 2009
    #1
    1. Advertising

  2. carlosp wrote:
    > Hello, just starting to work on pointers. Nice, but there is a very
    > antiintuitive thing that bothers me...
    >
    > I have a collection of objects (with no meaningful "==" operator on
    > them). I am using pointers to move them, sort them, etc. I want to
    > evaluate if two pointers point to the same object. However, using
    >
    > ptr1 == ptr2
    >
    > yields true even if they are different as reported in std!!! Here is a
    > piece of my code, and the output.
    >
    > if(test(i)==test(i+i)){
    > std::cerr << "pointer i "<< test(i) << ", pointer i+1 "<<
    > test(i+1) << "\n";
    > abort();
    > }
    >
    >
    > with the result corresponding to that part of the program
    >
    > pointer i 0x102280, pointer i+1 0x1022c8
    > Abort trap
    >
    >
    > Whats going on here?? Please illuminate me!


    I would avoid evaluating test() twice. Save the values in two variables, then
    use those two variables both for the if() and for the output statements. This
    way you are reasonably sure that the values used in the test and the values
    which are printed are identical. Even better would be to store the result of the
    "==" in a third variable.

    Said that, can we see the code of test()?

    Ciao,
    --
    Roberto Divia` Love at first sight is one of the greatest
    Dep:pH Bat:53 Mailbox:C02110 labour-saving devices the world has ever seen
    Route de Meyrin 385 ---------------------------------------------
    Case Postale Phone: +41-22-767-4994
    CH-1211 Geneve 23 CERN Fax: +41-22-767-9585
    Switzerland E-Mail:
     
    Roberto Divia, Mar 12, 2009
    #2
    1. Advertising

  3. carlosp

    carlosp Guest

    On Mar 12, 12:56 pm, Roberto Divia <> wrote:
    > carlosp wrote:
    > > Hello, just starting to work on pointers. Nice, but there is a very
    > > antiintuitive thing that bothers me...

    >
    > > I have a collection of objects (with no meaningful "==" operator on
    > > them). I am using pointers to move them, sort them, etc. I want to
    > > evaluate if two pointers point to the same object. However, using

    >
    > > ptr1 == ptr2

    >
    > > yields true even if they are different as reported in std!!! Here is a
    > > piece of my code, and the output.

    >
    > >        if(test(i)==test(i+i)){
    > >           std::cerr << "pointer i "<< test(i) << ", pointer i+1 "<<
    > > test(i+1) << "\n";
    > >           abort();
    > >         }

    >
    > > with the result corresponding to that part of the program

    >
    > > pointer i 0x102280, pointer i+1 0x1022c8
    > > Abort trap

    >
    > > Whats going on here?? Please illuminate me!

    >
    > I would avoid evaluating test() twice. Save the values in two variables, then
    > use those two variables both for the if() and for the output statements. This
    > way you are reasonably sure that the values used in the test and the values
    > which are printed are identical. Even better would be to store the result of the
    > "==" in a third variable.
    >
    > Said that, can we see the code of test()?
    >
    > Ciao,
    > --
    >         Roberto Divia`              Love at first sight is one of the greatest
    > Dep:pH Bat:53 Mailbox:C02110      labour-saving devices the world has ever seen
    > Route de Meyrin 385               ---------------------------------------------
    > Case Postale                                Phone:  +41-22-767-4994
    > CH-1211 Geneve 23 CERN                      Fax:    +41-22-767-9585
    > Switzerland                                 E-Mail:


    Thanks Roberto. I am using it++, and test is a "vector" in the it++
    sense. I do not want to compare the values to which the
    pointer points, because it involves real number and mommy told me
    never to compare real numbers. I just want to know if the two pointers
    test(i) and test(i+1) point to the same location. In any case, here I
    define test:

    itpp::Vec< QubitGate*> test;

    with QubitGate being defined by myself. Any other idea, or should I
    still work on your sugestion?


    Thanks,

    Carlos
     
    carlosp, Mar 12, 2009
    #3
  4. carlosp

    carlosp Guest

    On Mar 12, 1:07 pm, carlosp <> wrote:
    > On Mar 12, 12:56 pm, Roberto Divia <> wrote:
    >
    >
    >
    > > carlosp wrote:
    > > > Hello, just starting to work on pointers. Nice, but there is a very
    > > > antiintuitive thing that bothers me...

    >
    > > > I have a collection of objects (with no meaningful "==" operator on
    > > > them). I am using pointers to move them, sort them, etc. I want to
    > > > evaluate if two pointers point to the same object. However, using

    >
    > > > ptr1 == ptr2

    >
    > > > yields true even if they are different as reported in std!!! Here is a
    > > > piece of my code, and the output.

    >
    > > >        if(test(i)==test(i+i)){
    > > >           std::cerr << "pointer i "<< test(i) << ", pointer i+1 "<<
    > > > test(i+1) << "\n";
    > > >           abort();
    > > >         }

    >
    > > > with the result corresponding to that part of the program

    >
    > > > pointer i 0x102280, pointer i+1 0x1022c8
    > > > Abort trap

    >
    > > > Whats going on here?? Please illuminate me!

    >
    > > I would avoid evaluating test() twice. Save the values in two variables, then
    > > use those two variables both for the if() and for the output statements.. This
    > > way you are reasonably sure that the values used in the test and the values
    > > which are printed are identical. Even better would be to store the result of the
    > > "==" in a third variable.

    >
    > > Said that, can we see the code of test()?

    >
    > > Ciao,
    > > --
    > >         Roberto Divia`              Love at first sight is one of the greatest
    > > Dep:pH Bat:53 Mailbox:C02110      labour-saving devices the world has ever seen
    > > Route de Meyrin 385               ---------------------------------------------
    > > Case Postale                                Phone:  +41-22-767-4994
    > > CH-1211 Geneve 23 CERN                      Fax:    +41-22-767-9585
    > > Switzerland                                 E-Mail:

    >
    > Thanks Roberto. I am using it++, and test is a "vector" in the it++
    > sense. I do not want to compare the values to which the
    > pointer points, because it involves real number and mommy told me
    > never to compare real numbers. I just want to know if the two pointers
    > test(i) and test(i+1) point to the same location. In any case, here I
    > define test:
    >
    >       itpp::Vec< QubitGate*> test;
    >
    > with QubitGate being defined by myself. Any other idea, or should I
    > still work on your sugestion?
    >
    > Thanks,
    >
    > Carlos


    I did some progress. If I define

    QubitGate* a1, a2;
    and

    a1 = test(i);
    a2 = test(i+i);
    if( a1 == a2){

    the compiler complains at if( a1 == a2) :

    2dmera.cpp:1262: error: no match for 'operator==' in 'a1 == a2'
     
    carlosp, Mar 12, 2009
    #4
  5. carlosp

    ZikO Guest

    carlosp wrote:
    > if(test(i)==test(i+i)){


    > pointer i 0x102280, pointer i+1 0x1022c8


    > Whats going on here?? Please illuminate me!


    Try to post a minimum code which can be compiled. I don't know what
    test(i) does and what it returns, so I am a bit confused here. It seems
    that the test(i) function does not return the address pointed by the
    pointer i.
     
    ZikO, Mar 12, 2009
    #5
  6. carlosp

    Kai-Uwe Bux Guest

    carlosp wrote:


    > I did some progress. If I define
    >
    > QubitGate* a1, a2;


    The variable a2 in this line has type QubitGate, not type QubitGate*.

    Make that

    QubitGate* a1;
    QubitGate* a2;

    > and
    >
    > a1 = test(i);
    > a2 = test(i+i);
    > if( a1 == a2){
    >
    > the compiler complains at if( a1 == a2) :
    >
    > 2dmera.cpp:1262: error: no match for 'operator==' in 'a1 == a2'


    That's late. The compiler should complain at the assignment that mixes
    types.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Mar 12, 2009
    #6
  7. carlosp

    carlosp Guest

    On Mar 12, 1:48 pm, Victor Bazarov <> wrote:
    > Kai-Uwe Bux wrote:
    > > carlosp wrote:

    >
    > >> I did some progress. If I define

    >
    > >>       QubitGate* a1, a2;

    >
    > > The variable a2 in this line has type QubitGate, not type QubitGate*.

    >
    > > Make that

    >
    > >   QubitGate* a1;
    > >   QubitGate* a2;

    >
    > >>  and

    >
    > >>         a1 = test(i);
    > >>         a2 = test(i+i);
    > >>         if( a1 == a2){

    >
    > >> the compiler complains at if( a1 == a2) :

    >
    > >> 2dmera.cpp:1262: error: no match for 'operator==' in 'a1 == a2'

    >
    > > That's late. The compiler should complain at the assignment that mixes
    > > types.

    >
    > ..unless the QubitGate is actually defined to construct from a pointer
    > to an object.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    Dear All,

    Thanks for the help. Actually I had the code conceptually correct, but
    misdeclared a variable. My mistake can be summarized in the fact that

    QubitGate * a1, * a2;
    and
    QubitGate * a1, a2;
    are different.

    Thanks a lot!

    Carlos
     
    carlosp, Mar 12, 2009
    #7
    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. agent349
    Replies:
    5
    Views:
    9,732
    asingh0412
    Mar 10, 2009
  2. jalina
    Replies:
    2
    Views:
    291
    Fei Liu
    Apr 24, 2006
  3. J Caesar

    Comparing pointers

    J Caesar, Jun 5, 2007, in forum: C Programming
    Replies:
    23
    Views:
    662
    Walter Roberson
    Jun 7, 2007
  4. Charlie Zender

    Comparing pointers to NULL

    Charlie Zender, Aug 7, 2009, in forum: C Programming
    Replies:
    55
    Views:
    1,402
    Bartosz Wroblewski
    Aug 11, 2009
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    729
Loading...

Share This Page