Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to implement this function so that the vector can be passed backto the calling function?

Reply
Thread Tools

How to implement this function so that the vector can be passed backto the calling function?

 
 
xz
Guest
Posts: n/a
 
      03-13-2008
I wrote a function which looks like:

bool Xxx::findShortestPath(int xS, int yS, int xT, int yT,
vector<Vertex*> path) {
....
}

It meant to find a path, save the path in vector<Vertex*> path, and
pass the path back to the calling function.

And in some other function (say, main()), I called this function like:

int main() {
....
vector<Vertex*> path;
xxx.findShortestPath(0, 0, 9, 9, path);
// then print out the path
}


However, I just noticed that it failed to passed the path to the
main() function. That is, in the function findShortestPath(...), the
data has been loaded into *path*, but when it goes back to main(), all
data is lost.
I know this happens because of the memory management logic, but I am
not clear about how to fix this and get what I want.
Anybody gives some hints?
Thanks a lot!

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      03-13-2008
xz wrote:

> I wrote a function which looks like:
>
> bool Xxx::findShortestPath(int xS, int yS, int xT, int yT,
> vector<Vertex*> path) {
> ...
> }
>
> It meant to find a path, save the path in vector<Vertex*> path, and
> pass the path back to the calling function.
>
> And in some other function (say, main()), I called this function like:
>
> int main() {
> ...
> vector<Vertex*> path;
> xxx.findShortestPath(0, 0, 9, 9, path);
> // then print out the path
> }
>
>
> However, I just noticed that it failed to passed the path to the
> main() function. That is, in the function findShortestPath(...), the
> data has been loaded into *path*, but when it goes back to main(), all
> data is lost.


Make the signature:

bool Xxx::findShortestPath(int xS, int yS, int xT, int yT,
vector<Vertex*> & path);
^^^

> I know this happens because of the memory management logic, but I am
> not clear about how to fix this and get what I want.


It happens because you did "pass by value" not "pass by reference".



Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      03-14-2008
On Mar 13, 7:09 pm, Kai-Uwe Bux <(E-Mail Removed)> wrote:
> xz wrote:
> > I wrote a function which looks like:


> > bool Xxx::findShortestPath(int xS, int yS, int xT, int yT,
> > vector<Vertex*> path) {
> > ...
> > }


> > It meant to find a path, save the path in vector<Vertex*> path, and
> > pass the path back to the calling function.


> > And in some other function (say, main()), I called this function like:


> > int main() {
> > ...
> > vector<Vertex*> path;
> > xxx.findShortestPath(0, 0, 9, 9, path);
> > // then print out the path
> > }


> > However, I just noticed that it failed to passed the path to the
> > main() function. That is, in the function findShortestPath(...), the
> > data has been loaded into *path*, but when it goes back to main(), all
> > data is lost.


> Make the signature:


> bool Xxx::findShortestPath(int xS, int yS, int xT, int yT,
> vector<Vertex*> & path);
> ^^^


From a purely conceptual point of view, something like:

Fallible< std::vector< Vertex* > >
Xxx::findShortestPath( ... ) ;

would be better. In practice, if you call the function a lot,
and the paths tend to be fairly large, the performance
implications may become an issue.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM



Advertisments