Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Comparing pointers

Reply
Thread Tools

Comparing pointers

 
 
carlosp
Guest
Posts: n/a
 
      03-12-2009
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!
 
Reply With Quote
 
 
 
 
Roberto Divia
Guest
Posts: n/a
 
      03-12-2009
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
DepH 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:
 
Reply With Quote
 
 
 
 
carlosp
Guest
Posts: n/a
 
      03-12-2009
On Mar 12, 12:56*pm, Roberto Divia <Roberto.Di...@cern.ch> 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
> DepH 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.Di...@cern.ch


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
 
Reply With Quote
 
carlosp
Guest
Posts: n/a
 
      03-12-2009
On Mar 12, 1:07*pm, carlosp <> wrote:
> On Mar 12, 12:56*pm, Roberto Divia <Roberto.Di...@cern.ch> 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
> > DepH 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.Di...@cern.ch

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

 
Reply With Quote
 
ZikO
Guest
Posts: n/a
 
      03-12-2009
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.
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      03-12-2009
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
 
Reply With Quote
 
carlosp
Guest
Posts: n/a
 
      03-12-2009
On Mar 12, 1:48*pm, Victor Bazarov <v.Abaza...@comAcast.net> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Comparing pointers to NULL Charlie Zender C Programming 55 08-11-2009 04:00 PM
Comparing two strings with arrays and pointers agent349 C++ 5 03-10-2009 04:23 AM
Comparing pointers J Caesar C Programming 23 06-07-2007 05:49 PM
comparing objects using pointers jalina C++ 2 04-24-2006 04:15 PM



Advertisments