简体   繁体   English

如何使用智能指针(例如auto_ptr r shared_ptr)在Linux上用C ++生成链接列表数据结构?

[英]How to use smart pointer (e.g. auto_ptr r shared_ptr) to generate a link list data structure in C++ on Linux?

This is a C++ programming problem. 这是一个C ++编程问题。

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. 代码有效,但内存泄漏,因为我使用“new”为列表分配每个新节点。

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 代码有分段错误,因为auto_ptr传输指针的所有权以便之后

listRoot = listRoot->nextData 

the linked list is broken and the listRoot->nextData is NULL. 链表已中断,listRoot-> nextData为NULL。

I have tried tr1::shared_ptr and waek_ptr 我试过tr1 :: shared_ptr和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&) listPtSharedptr.cpp:63:错误:从linkListClass *转换为非标量类型std :: tr1 :: weak_ptr请求listPtSharedptr.cpp:65:错误:不匹配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:注意:候选者是: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. 这是对你的函数“buildLinkList”的修改,它现在应该可以工作了。 With one difference, though. 但有一点不同。 You were creating a FIFO list. 您正在创建一个FIFO列表。 This version creates a LIFO list. 此版本创建LIFO列表。 I guess FIFO is difficult to create with just auto_ptrs. 我猜FIFO只用auto_ptrs很难创建。 You may try, afterwards, using this working example to use shared_ptrs and convert it to a FIFO list. 之后,您可以尝试使用此工作示例来使用shared_ptrs并将其转换为FIFO列表。

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;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM