pointer return: address correct / value nonesense

H

Huibuh

I start hating RETURN!!!

list<happy> //#include <list>// is searched via iterator (ptr) so long
until the argument "number" of the class "happy" is equal now.
The function "findhappy" works fine, finds the right pointer "nownumber"
with correct values.

Behind "return" the memory address remains correct but the values are
wrong (a huge negative number and all the same).

Tried to define "nownumber" and "ptr" as locals OR
to submit them as parameter to the function "findhappy" OR
to define them globally. In any case I always have the same problem.
Any solutions????

Thanks in advance

//THIS WAS MY LAST TRY
happy* findhappy (list<happy> a, int now)
{
ptr=a.begin();
nownumber=ptr.operator ->();
while (nownumber->number!=now)
{
ptr=ptr.operator ++();
nownumber=ptr.operator ->();
}
return nownumber;
}
 
L

Leor Zolman

I start hating RETURN!!!
I. Yay. Yay.
list<happy> //#include <list>// is searched via iterator (ptr) so long
until the argument "number" of the class "happy" is equal now.
The function "findhappy" works fine, finds the right pointer "nownumber"
with correct values.

Behind "return" the memory address remains correct but the values are
wrong (a huge negative number and all the same).

Tried to define "nownumber" and "ptr" as locals OR
to submit them as parameter to the function "findhappy" OR
to define them globally. In any case I always have the same problem.
Any solutions????

Thanks in advance

//THIS WAS MY LAST TRY
happy* findhappy (list<happy> a, int now)
{
ptr=a.begin();
nownumber=ptr.operator ->();
while (nownumber->number!=now)
{
ptr=ptr.operator ++();
nownumber=ptr.operator ->();
}
return nownumber;
}

A bunch of issues, which at this point I actually find easier to cover in
code than in English, so here's something to nudge you in the right
direction, I hope. Note I typedef-ed "happy" as "int" in order to focus on
things other than the way you deal with the container's value_type.

I left the logic returning a pointer, the way you had it, but you might
want to look into using STL algorithms such as std::find, which return
iterators, as replacements for your findhappy-type functions. So here we
go:


// happy.cpp

#include <iostream>
#include <list>
using namespace std;
typedef int happy;

happy* findhappy (list<happy>& a, int now)
{
list<happy>::iterator ptr;

for (ptr = a.begin(); ptr != a.end(); ++ptr)
if (*ptr == now)
return &*ptr;
return 0;
}

int main()
{
list<int> li;
li.push_back(1);
li.push_back(5);
li.push_back(9);
li.push_back(19);

if (happy *p = findhappy(li, 9))
cout << "found it! Value is:" << *p << endl;
else
cout << "didn't find it." << endl;

return 0;
}

HTH,
-leor
 
D

David Harmon

On Fri, 9 Apr 2004 18:43:15 +0200 in comp.lang.c++, "Huibuh"
I start hating RETURN!!!

I think that's a new one.
Behind "return" the memory address remains correct but the values are
wrong (a huge negative number and all the same).
//THIS WAS MY LAST TRY
happy* findhappy (list<happy> a, int now)

You do NOT want to make a copy of your list to search! Should be

happy* findhappy (list<happy> & a, int now)

You are returning a pointer to something in the COPY of your original
list, and all results from there on are junk. See the thread "Strange
pointer results" from yesterday for more discussion of the same trouble!
 
B

Buster

Huibuh said:
I start hating RETURN!!!

You'd be screwed without it.

// What you are about to see is a reconstruction.
// It's also a complete, compilable translation unit.
// Please check "How to post" in the FAQ.

#include <list>
#include <iostream>
#include <ostream>

struct happy
{
int number;
happy (int n = 0) : number (n) { }
};

// //THIS WAS MY LAST TRY
// // *** I hope you can follow my comments.

// happy* findhappy (list<happy> a, int now)
// // *** This copies the argument into the parameter element-
// // *** by-element. We often pass large objects _by reference_
// // *** rather than _by value_ (unless we need a local _copy_

happy * findhappy (std::list <happy> & a, int now)
{

// {
// ptr=a.begin();
// // *** We shan't need this iterator outside this scope
// // *** so we can declare it here:

std::list <happy>::iterator iter = a.begin ();

// nownumber=ptr.operator ->();
// *** Whoah there! "happy * nownumber = & * ptr;" would be easier
// *** to read; but wait a minute, we're not going to do it like this.

// while (nownumber->number!=now)
// {
// ptr=ptr.operator ++(); // *** Just "++ ptr;"
// nownumber=ptr.operator ->(); // *** Just "nownumber = & * ptr;"
// }
// return nownumber;
// }
// // *** The logic of this loop is all wrong. Get out a pencil
// // *** and paper and work out what would happen if the value
// // *** "now" is not present in a happy object in the list.
// // ***
// // *** The following works OK. We don't need to transform the
// // *** iterator into a pointer until just before we return
// // *** from the function, so that's what I do here.
// // *** I notice that we're simulating a "for" loop here.
// // *** Maybe you can change it for the real thing.

while (iter != a.end ())
{
if (iter->number == now)
return & (* iter );
++ iter;
}
return 0;
}

int main ()
{
std::list <happy> a;
a.push_back (happy (2));
a.push_back (happy (3));
a.push_back (happy (5));

if (happy * p = findhappy (a, 3))
{
std::cout << "Found a happy object with number "
<< p->number << ".\n";
}
else
{
std::cout << "Didn't find a matching happy object.\n";
}
}
 
H

Huibuh

happy* findhappy (list<happy> a, int now)

"list<happy> a" is a COPY, how could I forget about the &?!!!
Thanks to all of you, now I'm working on the rest of your tips.

Happy eastern, mine is safed now. :)
 

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

No members online now.

Forum statistics

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

Latest Threads

Top