我需要生成一个列表并返回一个指针,以便其他函数可以使用该列表。该代码工作,但有内存泄漏,因为我使用“新”分配列表的每个新节点。
使用列表后,我必须释放内存。
我的代码如下:
#include
#include
#include
using namespace std;
class linkListClass
{
private:
int data;
auto_ptr nextData;
public:
auto_ptr buildLinkList(const int size);
int getData(){return data;};
int printListBackward(auto_ptr listroot);
};
inline auto_ptr 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(0) ;
trueRoot = listRoot ; // transfer ownership
}
else{
listRoot->nextData = auto_ptr (new linkListClass); // segmentation fault
listRoot->data = rand()%10 ;
listRoot = listRoot->nextData ;
}
}
else
listRoot->nextData = auto_ptr(0) ;
}
cout << "the built list has " << size << " data \n\n" ;
return trueRoot;
}
inline int linkListClass::printListBackward(auto_ptr listroot)
{
int counter =0 ;
stack 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 myRoot ; //= linkListClass::buildLinkList(listSize);
myRoot = linkListObj.buildLinkList(listSize);
linkListObj.printListBackward(myRoot);
return 0;
}
// EOF
的代码有段错误,因为auto_ptr的传递指针对象的所有权,使 后
listRoot = listRoot->nextData
链表被打破,listRoot-> nextData一片空白。
我已经试过TR1 :: shared_ptr的和waek_ptr
tr1::weak_ptr wp1 = listRoot->nextData;
listRoot = wp1.lock() ;
listRoot = listRoot->nextData ;
但我得到了编译错误:
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&)
任何帮助将不胜感激。
的感谢!
+0
我不认为你想传递一个auto_ptr <>到printLastBackward(),而只是传递一个常规指针。 –