This is a C++ programming problem.
I need to generate a list and return a pointer so that other functions can use the list. The code works but has memory leak because I use "new" to allocate each new node for the list.
After using the list I have to release the memory.
My code is as follows:
#include <iostream>
#include <stack>
#include <memory>
using namespace std;
class linkListClass
{
private:
int data;
auto_ptr<linkListClass> nextData;
public:
auto_ptr<linkListClass> buildLinkList(const int size);
int getData(){return data;};
int printListBackward(auto_ptr<linkListClass> listroot);
};
inline auto_ptr<linkListClass> linkListClass::buildLinkList(const int size)
{
linkListClass *trueRoot;
linkListClass *listRoot = new linkListClass ;
linkListClass *trueRoot;
linkListClass *listRoot = new linkListClass ;
// data is random int
for (int i = 0; i < size ; ++i)
{
if (i < size -1)
{
if (i == 0 )
{
listRoot->data = rand()%10 ;
listRoot->nextData = auto_ptr<linkListClass>(0) ;
trueRoot = listRoot ; // transfer ownership
}
else{
listRoot->nextData = auto_ptr<linkListClass> (new linkListClass ); // segmentation fault
listRoot->data = rand()%10 ;
listRoot = listRoot->nextData ;
}
}
else
listRoot->nextData = auto_ptr<linkListClass>(0) ;
}
cout << "the built list has " << size << " data \n\n" ;
return trueRoot;
}
inline int linkListClass::printListBackward(auto_ptr<linkListClass> listroot)
{
int counter =0 ;
stack<int> outputStack;
cout << "print the list forward \n\n" ;
//if (listroot != NULL)
cout << "print the list forward \n\n" ;
//if (listroot != NULL)
if (listroot.get() != 0)
{
do
{
try{
cout << listroot->getData() << " \t " ;
outputStack.push(listroot->data);
listroot = listroot->nextData;
++counter;
cout << "in printListBackward counter is " << counter << endl;
//if (listroot == 0 ) break;
}
catch(exception& e)
{
cout << "an error is " << e.what() << endl;
return 1;
}
//}while(listroot != 0);
}while(listroot.get() != 0);
cout << "in printListBackward outof do while \n\n " << endl ;
}
else
{
cout << "the input list is null \n\n" << endl;
return 1;
}
cout << endl ;
cout << "there are" << counter << " data in the list \n\n " << endl ;
cout << "print the list backward \n\n" ;
if (outputStack.empty() == 1)
{
cout << "the ouytput queu is empty \n\n " << endl ;
cout << "the ouytput queu is empty \n\n " << endl ;
return 1;
}
else
{
do
{
cout << outputStack.top() << " \t" ;
outputStack.pop();
}while(outputStack.empty() == 0);
}
cout << endl;
cout << "there are" << counter << " data in the list \n\n " << endl ;
return 0 ;
}
int main()
{
const int listSize = 5;
linkListClass linkListObj;
auto_ptr<linkListClass> myRoot ; //= linkListClass::buildLinkList(listSize);
myRoot = linkListObj.buildLinkList(listSize);
linkListObj.printListBackward(myRoot);
return 0;
}
// EOF
The code has segmentation fault because the auto_ptr transfer pointee's ownership so that after
listRoot = listRoot->nextData
the linked list is broken and the listRoot->nextData is NULL.
I have tried tr1::shared_ptr and waek_ptr
tr1::weak_ptr<linkListClass> wp1 = listRoot->nextData;
listRoot = wp1.lock() ;
listRoot = listRoot->nextData ;
but I got compile error:
listPtSharedptr.cpp:63: error: conversion from linkListClass* to non-scalar type std::tr1::weak_ptr requested listPtSharedptr.cpp:65: error: no match for operator= in listRoot = listRoot.std::tr1::shared_ptr<_Tp>::operator-> with _Tp = linkListClass->linkListClass::nextData /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/boost_shared_ptr.h:486: note: candidates are: std::tr1::shared_ptr& std::tr1::shared_ptr::operator=(const std::tr1::shared_ptr&)
Any help will be appreciated .
thanks !
Here's a modification to your function "buildLinkList" which should work now. With one difference, though. You were creating a FIFO list. This version creates a LIFO list. I guess FIFO is difficult to create with just auto_ptrs. You may try, afterwards, using this working example to use shared_ptrs and convert it to a FIFO list.
inline auto_ptr<linkListClass> linkListClass::buildLinkList(const int size)
{
auto_ptr<linkListClass> trueRoot(0);
auto_ptr<linkListClass> listRoot(0);
// data is random int
for (int i = 0; i < size ; ++i)
{
listRoot = auto_ptr<linkListClass> (new linkListClass );
listRoot->data = random()%10 ;
listRoot->nextData = trueRoot;
trueRoot = listRoot;
}
cout << "the built list has " << size << " data \n\n" ;
return trueRoot;
}