2012-05-07 5 views
0

これはC++プログラミングの問題です。Linux上のC++でスマートポインタ(auto_ptr r shared_ptrなど)を使用してリンクリストデータ構造を生成する方法を教えてください。

他の関数がリストを使用できるように、リストを生成してポインタを返す必要があります。コードは機能しますが、リストに新しいノードを割り当てるために "new"を使用するため、メモリリークが発生します。

リストを使用した後、私はメモリを解放する必要があります。次のように

私のコードは次のとおりです。

#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 

listRoot = listRoot->nextData 

後、リンクされたリストが壊れとlistRoot-> nextDataされるようにコードがauto_ptrを転送ポインタの指示の所有ので、セグメンテーションフォールトを持っています無効である。

私はTR1 :: shared_ptrのを試してみましたが、

tr1::weak_ptr<linkListClass> wp1 = listRoot->nextData; 
    listRoot = wp1.lock() ; 
    listRoot = listRoot->nextData ; 

をwaek_ptrが、私はエラーをコンパイル持っている:

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()に渡したいとは思わないが、代わりに通常のポインタを渡すだけです。 –

答えて

2

ここでは機能する "buildLinkList"関数の変更があります。 しかし、1つの違いがあります。あなたはFIFOリストを作成していました。このバージョンは、LIFOリストを作成します。 FIFOはauto_ptrsだけでは作成が難しいと思います。その後、この動作例を使用して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; 
} 
関連する問題