Comparing pointers

C

carlosp

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!
 
R

Roberto Divia

carlosp said:
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: (e-mail address removed)
 
C

carlosp

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: (e-mail address removed)

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
 
C

carlosp

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'
 
Z

ZikO

carlosp said:
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.
 
K

Kai-Uwe Bux

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
 
C

carlosp

..unless the QubitGate is actually defined to construct from a pointer
to an object.

V

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top