2011-11-30 21 views
0

リンクリストのベクトルを作成するために、ダブルリンクリストクラスを作成しました。プログラムの最後には、エラーが発生しているようです。 malloc: *** error for object 0x100100be0: pointer being freed was not allocated デストラクタと関係していると仮定しています。また、これはXcodeが私を指している場所です。これを回避するには?私のデストラクタはうまくいくと思うが、私は間違っていると思う。ダブルリンクリストでエラーが発生しました '解放されたポインタが割り当てられていません'

テストファイル:

#include <iostream> 
#include <string> 
#include "Vector.h" 
#include "doubleLL.h" 

using namespace std; 

int main (int argc, const char * argv[]) 
{ 

    Vector<double_llist<string> > listWords(27); 
    double_llist<string> numbers; 
    numbers.push_back("one"); 
    numbers.push_back("two"); 
    numbers.push_back("three"); 
    listWords[0] = numbers; 
    listWords[0].print(); 
} 

doubleLL.h:Vector.hが含まれていなかったので

#ifndef DOUBLELL_H 
#define DOUBLELL_H 
#include <iostream> 
using namespace std; 

template <class T> 
class double_llist { 
private: 
    struct node { 
     T data; 
     node* prev; 
     node* next; 
     node(T t, node* p, node* n) : data(t), prev(p), next(n) {} 
     int count; 
    }; 
    node* head; 
    node* tail; 

public: 
    double_llist() : head(NULL), tail (NULL) {} 
    template<int N> 
    double_llist(T (&arr) [N]) : head(NULL), tail (NULL) 
    { 
     for(int i(0); i != N; ++i) 
      push_back(arr[i]); 
    } 
    bool empty() const { return (!head || !tail); } 
    operator bool() const { return !empty(); } 
    void push_back(T); 
    void push_front(T); 
    T pop_back(); 
    void removeNode(node *); 
    void print(); 

    node* search(T data) { 
     node *tempNode; 
     if (head == NULL) { 
      // List is empty 
      return NULL; 
     } else { 
      tempNode = head; 
      while (tempNode != NULL) { 
       if (tempNode->data == data) { 
        tempNode->count += 1; 
        if (tempNode->count >= 4) { 
         // Push tempNode to front of linked list 
         push_front(tempNode->data); 
         head->count = tempNode->count; 
         removeNode(tempNode); 
        } 
        return tempNode; 
       } else { 
        tempNode = tempNode->next; 
       } 
      } 
     } 
     return NULL; 
    } 

    ~double_llist() 
    { 
     while(head) 
     { 
      node *temp(head); 
      head = head->next; 
      delete temp; 
     } 
    } 

    double_llist& operator = (const double_llist& other) 
    { 
     if (this == &other) { 
      return *this; 
     } 
     while (!empty()) { 
      pop_back(); 
     } 
     for (node *itr = other.head->next; itr != other.tail; ++itr) { 
      tail = new node(other.head->data, itr, NULL); 
     } 
     return *this; 

    } 


    double_llist(const double_llist& other) 
    { 
     head = new node; 
     tail = new node; 
     head->tail = tail; 
     tail->prev = head; 
     *this = other; 
    } 
}; 

template <class T> 
void double_llist<T>::push_back(T data) 
{ 
    tail = new node(data, tail, NULL); 
    if(tail->prev) 
     tail->prev->next = tail; 

    if(empty()) 
     head = tail; 
} 

template <class T> 
void double_llist<T>::push_front(T data) { 
    head = new node(data, NULL, head); 
    if(head->next) 
     head->next->prev = head; 

    if(empty()) 
     tail = head; 
} 


template <class T> 
T double_llist<T>::pop_back() 
{ 
    node* temp(tail); 
    T data(tail->data); 
    tail = tail->prev ; 

    if(tail) 
     tail->next = NULL; 
    else 
     head = NULL ; 

    delete temp; 
    return data; 
} 

template <class T> 
void double_llist<T>::removeNode(node *n) { 
    if(n == this->head) { 
     this->head=this->head->next; 
     this->head->prev = NULL; 
    } else if (n==this->tail) { 
     this->tail=this->tail->prev; 
     this->tail->next = NULL ; 
    } else { 
     n->prev->next = n->next; 
     n->next->prev = n->prev; 
    } 
} 

template <class T> 
void double_llist<T>::print() { 
    node* temp; 
    temp = this->head; 
    int i = 0; 
    while(temp != NULL) 
    { 
     if (i < 3) { 
      cout << temp->data << endl; 
      temp=temp->next; 
      ++i; 
     } else { 
      return; 
     } 
    } 
    cout << endl; 
    return; 
} 

#endif 

エラーは、doubleLLから来ているようです。私が正しい方向に向けるのを助ける必要があるなら、私に知らせてください。

ありがとうございます!

+1

ステップ、それはコードを見て、判断するためにあまりにも複雑だ..私は今、いくつかの時間のためにされている – Nim

+0

、私は、無駄にそれをmallocのを試してみました1、2、3を印刷しますが、最後にはクラッシュします。 –

+0

@Mullerあなたは私が提案したものを試しましたか? –

答えて

8

私はあなたのコードを踏んで、多くのオブジェクトコピーが作成されて破棄されたように見えますが、コピーが正しく行われていないため、すでに破棄されているメモリが再び削除されます。

これらを適切に実装すれば、問題は解決します。

EDIT:

私はちょうどそれらの基本的な実装を完了していない:

double_llist& operator = (const double_llist& other) 
{ 
    head = NULL; 
    tail = NULL; 
    return *this; 
} 
double_llist(const double_llist& other) 
{ 
    head = NULL; 
    tail = NULL; 
} 

コードがクラッシュしなくなりました。

SECOND EDIT:デバッガとこの貫通

double_llist& operator = (const double_llist& other) 
{ 
    head = NULL; 
    tail = NULL; 
    node* otherNode = other.head; 
    while (otherNode) 
    { 
    push_back(otherNode->data); 
    if (otherNode == other.tail) 
    break; 
    otherNode = otherNode->next; 
    } 
    return *this; 
} 
double_llist(const double_llist& other) 
{ 
    head = NULL; 
    tail = NULL; 
    node* otherNode = other.head; 
    while (otherNode) 
    { 
    push_back(otherNode->data); 
    if (otherNode == other.tail) 
    break; 
    otherNode = otherNode->next; 
    } 
} 
+2

+1。私はそれを投稿しようとしていた。 C++ 03の場合は3の規則、C++の場合は5の規則11。 – Nawaz

+0

+1、もう...私の答えを削除しました.. – Nim

+0

私はあなたがそれをどのようにしたかを見ていますが、私がそれらを使うと、二重リンクリストのVectorからの出力はなくなりました。私は3世代目の公衆の下でコピーコンストラクタを考えましたか? –

関連する問題