2016-04-12 26 views
1
Debug Assertion Failed! 

Program: ...nts\Visual Studio 2015\Projects\Project 5\Debug\Project 5.exe 
File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp 
Line: 892 

Expression: is_block_type_valid(header->_block_use) 

For information on how your program can cause an assertion 
failure, see the Visual C++ documentation on asserts. 

(Press Retry to debug the application) 

Program her şeyi doğru şekilde çalıştırır ve çıkarır ve bu hatayı atar. Bunun ne anlama geldiğine veya bulmaya veya düzeltmeye nasıl devam edeceğine dair iyi bir açıklama bulamadım. İşte (inanılmaz çirkin ve kötü yazılmış) kodunun tam kopyasıdır:Hata Ayıklama Onaylama Başarısız! İfade: is_block_type_valid (başlık -> _ block_use)

#include <iostream> 
using namespace std; 

/* a class for storing a Binary Tree */ 
template <class Type> 
class BinaryTree { 
protected: 
    Type parentArray[10]; 
    Type childArray[10]; 
public: 
    BinaryTree(); 
    BinaryTree(int& k); 
    ~BinaryTree(); 
    BinaryTree(BinaryTree<Type>& bt); 
    void operator= (BinaryTree<Type>& bt); 
    friend ostream& operator<< (ostream& s, BinaryTree<Type> bt) { 
     s << "[ "; 
     bt.inorder(bt.getRoot()); 
     s << "]" << endl; 
     return s; 
    }; 
    int size(); 
    int height(); 
    int getLeft(int k); 
    int getRight(int k); 
    void preorder(int k); 
    void inorder(int k) { 

     // do I have a left child? 
     if ((getLeft(k)) != -1) { 
      // if yes inorder (left child) 
      inorder(getLeft(k)); 
     }; 
     // output k 
     cout << k << " "; 
     // do I have a right child? 
     if ((getRight(k)) != -1) { 
      // if yes inorder (right child) 
      inorder(getRight(k)); 
     }; 
    }; 
    void postorder(int k); 
    void setRoot(Type& val); 
    void setParent(Type* child, Type* parent); 
    void setLeft(Type& val); 
    void setRight(Type& val); 
    int getRoot(); 
}; 

/* default constructor */ 
template <class Type> 
BinaryTree<Type>::BinaryTree() { 
    parentArray = new ArrayClass<Type>(); 
    childArray = new ArrayClass<Type>(); 
}; 

/* non-empty constructor */ 
template <class Type> 
BinaryTree<Type>::BinaryTree(int& k) { 
// parentArray = new Type[k]; 
// childArray = new Type[k]; 
}; 

template <class Type> 
BinaryTree<Type>::~BinaryTree() { 
    delete[] parentArray; 
    delete[] childArray; 
}; 

template <class Type> 
BinaryTree<Type>::BinaryTree(BinaryTree<Type>& bt) { 
    for (int i = 0; i < bt.size(); i++) { 
     parentArray[i] = bt.parentArray[i]; 
     childArray[i] = bt.childArray[i]; 
    }; 
}; 

template <class Type> 
void BinaryTree<Type>::operator= (BinaryTree<Type>& bt) { 

}; 


/* return the size of the tree using the length of the parent array */ 
template <class Type> 
int BinaryTree<Type>::size() { 

    return (sizeof(parentArray)/sizeof(*parentArray)); 
}; 


template <class Type> 
int BinaryTree<Type>::height() { 
    return 5; 
}; 

template <class Type> 
int BinaryTree<Type>::getLeft(int k) { 

    // if the parent array value of the given number is k and 
    // the child array value indicates it is a left child 
    for (int i = 0; i < size(); i++) { 
     if ((parentArray[i] == k) && (childArray[i] == 0)) { 

      // return that value 
      return i; 
     }; 
    }; 
    return -1; 
}; 

template <class Type> 
int BinaryTree<Type>::getRight(int k) { 

    // if the parent array value of the given number is k and 
    // the child array value indicates it is a right child 
    for (int i = 0; i < size(); i++) { 
     if ((parentArray[i] == k) && (childArray[i] == 1)) { 

      // return that value 
      return i; 
     }; 
    }; 
    return -1; 
}; 

template <class Type> 
void BinaryTree<Type>::preorder(int k) { 
    // output k 
    cout << k << " "; 
    // do I have a left child? 
    if ((getLeft(k)) != -1) { 
     // if yes preorder left child 
     preorder(getLeft(k)); 
    }; 
    // do I have a right child? 
    if ((getRight(k)) != -1) { 
     // if yes preorder right child 
     preorder(getRight(k)); 
    }; 
}; 

template <class Type> 
void BinaryTree<Type>::postorder(int k) { 
    // do I have a left child? 
    if ((getLeft(k)) != -1) { 
     // if yes inorder (left child) 
     inorder(getLeft(k)); 
    }; 
    // do I have a right child? 
    if ((getRight(k)) != -1) { 
     // if yes inorder (right child) 
     inorder(getRight(k)); 
    }; 
    // output k 
    cout << k << " "; 
}; 

template <class Type> 
void BinaryTree<Type>::setRoot(Type& val) { 
    // if the given value is the root of the tree then set 
    // its index in the parent and child arrays to -1 
    parentArray[val] = -1; 
    childArray[val] = -1; 
}; 

template <class Type> 
void BinaryTree<Type>::setParent(Type* child, Type* parent) { 

    // set a given value as the parent of a given value 
    parentArray[(*child)] = *parent; 
}; 

template <class Type> 
void BinaryTree<Type>::setLeft(Type& val) { 

    // set a given value in the child array to indicate a left child 
    childArray[val] = 0; 
}; 

template <class Type> 
void BinaryTree<Type>::setRight(Type& val) { 

    // set a given value in the child array to indicate a right child 
    childArray[val] = 1; 
}; 

template <class Type> 
int BinaryTree<Type>::getRoot() { 

    // find the root value of the tree 
    for (int i = 0; i < size(); i++) { 
     if (parentArray[i] == -1) { 

      // and return it 
      return i; 
     }; 
    }; 
}; 

int main() { 
    int* val1 = new int; 
    int* val2 = new int; 
    int* val3 = new int; 
    int count; 

    cin >> count; 

    BinaryTree<int> bt(count); 

    for (int i = 0; i < count; i++) { 
     cin >> *val1; 
     cin >> *val2; 
     cin >> *val3; 

     if (i == 0) { 
      bt.setRoot(*val1); 
     }; 

     if (*val2 != -1) { 
      bt.setParent(val2, val1); 
      bt.setLeft(*val2); 
     } 
     if (*val3 != -1) { 
      bt.setParent(val3, val1); 
      bt.setRight(*val3); 
     } 

     val1 = new int; 
     val2 = new int; 
     val3 = new int; 
    }; 

    cout << bt.size() << endl; 
    bt.postorder(bt.getRoot()); 
    cout << endl; 
    bt.preorder(bt.getRoot()); 
    cout << endl; 

    delete val1; 
    delete val2; 
    delete val3; 
}; 

BinaryTree sınıfındaki fonksiyonların bazıları henüz bitmiş ve sadece test için onları dolgu çöp sahip değildir.

delete[] parentArray; 
delete[] childArray; 

Maalesef sınıfının kurucular değildir önemlidir new bu diziler herhangi:

+0

"Ana" içindeki "for" döngüsü bellek sızdırıyor. val1', val2' val3', döngünün alt kısmında yenidir, ancak asla serbest bırakılmaz. –

+0

Öneri: Test etmeden önce daha sık test edin veya daha küçük kod parçaları yazın. Bir fonksiyonun yeni kod değerini test ederken, genellikle hatanın nerede olduğunu bulmak çok kolay. Kurucu ve yıkıcı yaz ve test et. Bir yöntem ekle. Ölçek. Başka bir yöntem ekle ve test et. Durulama. Tekrar et. – user4581301

cevap

1

Sizin BinaryTree yıkıcı her zaman emin kılar. Bunun gibi, yıkıcı bir çift başlatılmamış çöp işaretçisi delete denemeye başlar.

Ayrıca bu sınıf violates the Rule Of The Three da mümkündür, ancak bunu yeterince analiz etmedim.

DÜZENLEME: yorumlarda belirtildiği gibi, bunlar işaretçiler değildir; bu yüzden bu yanlıştır, ama başka sebeplerden dolayı.

+3

Bu diziler işaretçi değildir. Onlara 'yeni' gerek yok. Ve silinmemeleri gerekir. – paddy

İlgili konular