hey, I got a question regarding functions returning a linked list. Here's the data structure of my linked list (just in case it deviates from the standard way of doing it)

template <class T> class Link
{
	private:
		unsigned int size;
		Link<T> *nextRow;
		Cell<T> *firstCell;

	public:
		Link();
               ~Link();

		//functions
};

So each Link only consists of it's size, a pointer to the next link, and a pointer to the link's first cell, here is what the problem is I believe. If I have a function that returns say a Link<std::string>, naturally it only returns the Link element (as far as I can tell). I get an access violation if I do something along the lines of this:

Link<std::string> filled;
//fill filled with data

Link<std::string> link = TransferLink(filled);

//other file
template <class T> Link<T> Link<T>::TransferLink(Link<T> start)
{
          Link<T> temp = start;
          return temp;
}

Please note I do have '=' overloaded for Link, but I used this to isolate the problem of transferring the data between functions. What I think happens is c++ makes a temporary variable holding the value of "temp" calls the destructor on the actual variable "temp" and then sends the temporary variable to the "=" assignment. This would explain the access violation since calling the destructor on "temp" would destroy all of the cells in it, but it wouldn't update the size or the pointers of the temporary variable. That's just my thought on what happens. I have gotten it to work by doing the following, but it's really annoying to implement for larger functions:

template <class T> Link<T> Link<T>::TransferLink(Link<T> start)
{
        Link<T> *temp = new Link<T>();
	(*temp) = testMatrix[end];
	return (*temp); //no destructor is called because of the 'new' operator
}

and then doing

Link<std::string> link = TransferLink(filled);

but this causes memory leaks obviously since my "=" makes new cells with the values of the right side parameter and then "temp" is never destroyed. So I made this:

template <class T> Link<T>& Link<T>::TransferLink(Link<T> start)
{
        Link<T> *temp = new Link<T>();
	(*temp) = testMatrix[end];
	return (*temp); //no destructor is called because of the 'new' operator
}

and then doing

Link<std::string> link |= TransferLink(filled);

with "|=" being another overloaded operator the mimics the effects of "=" except that it destroys the variable on the right side.

So here's my question finally, I guess, is there any other way to transfer linked lists between functions than what I have done? This is fairly annoying to do and I've made a function list that mimics those of a vector because that's what I was using previously for all my functions. But when I needed to make the switch I wanted to make the transition fairly painless, and this is being a pain.

Thanks!
~J

any ideas? I tried to be as clear as possible, but I understand if my question was lost in the pretext, so I'll see if I can straighten things out.

In my functions I create a List<T> that gets filled with data but when I try to return that list, I get runtime errors from my pointers in the data structure. Here's what I think is happening:

say I try to return a List of type int ( 'List<int> integers' we'll call it) I think that the data in it gets destroyed in the moving between functions

List<int> integers:
size = 5;
nextRow = NULL;
firstCell = 0x0abc0 //Cell object holds pointer for each subsequent cell

when I say "return integers" I think a temporary variable (call it 'tempIntegers') gets created that hold the values of 'integers'

List<int> tempIntegers
size = 5;
nextRow = NULL;
firstCell = 0x0abc0

then since 'integers' was created inside the function, c++ calls 'integers' destructor since it left it's scope. ~List() deallocates all it's cells etc. so now when 'tempIntegers' is passed back here's what it has:

List<int> tempIntegers
size = 5; //should be 0 since all cells were destroyed
nextRow = NULL;
firstCell = 0x0abc0; //has been deallocated by 'integers' destructor

this causes errors as you can imagine if I try to loop through it's elements since the pointers no longer point to memory alloted to my program.

Any suggestions on how I can remedy this? In my original post I showed my current workaround, but it seems like a cheap hack and doesn't keep a very convenient interface for my data structure, so I'd rather find a better way, if anyone can think of one. Thanks!

~J

why not just pass the linked list by reference

Link<std::string> filled;
//fill filled with data

TransferLink(filled);

//other file
void  Link<T>::TransferLink(Link<T>& start)
{
   // fill in the linked list here
}

that is my most recent effort, and it does solve the problems with the destructor, but it obliterates the readability of the code (at least with my current function naming convention). Especially since many of my functions take ~4 arguments already. I was going to continue with that method unless someone here thought of a better way to do it that would stick closer to the methods of dealing with standard data structures.

Thanks AD, I'll leave this open for a bit more in case someone else has an idea, but I'll close it soon since we've come to the same conclusion independently.

~J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.