我已经为我的class类实现了一个List,并且在程序试图删除我的List时检测到HEAP CORRUPTION。它在析构函数中进入循环,并删除第一个元素,然后当他第二次时,并试图摧毁“新”头,我得到堆腐败错误。我不明白为什么会出错。任何帮助将非常感激。HEAP CORRUPTION DETECTED:Normal Block(#151)at 0x00DB4E70
Word.h:
#include <cstring>
class Word
{
protected:
char* word;
char* type;
public:
Word();
Word(char);
Word(char *);
Word(char *, int);
Word(const Word&);
Word& operator=(const Word &);
void setWord(char);
void setWord(char *);
void setWord(char*, int);
void setType(char);
void setType(char*);
void setType(char*, int);
~Word();
};
WordList.h:
#include "Word.h"
#include <cstdlib>
class WordList
{
public:
struct Node
{
Word data;
Node *next, *prev;
//~Node();
};
Node *head;
Node *tail;
WordList();
~WordList();
void add(Word &d);
};
WordList.cpp:
#include "WordList.h"
WordList::WordList(void)
{
head = nullptr;
tail = nullptr;
}
WordList::~WordList(void)
{
while(head != nullptr)
{
Node *n = head->prev;
delete head;
head = n;
}
}
void WordList::add(Word &d)
{
Node *n = new Node;
n->data = d; // I overload =, and it copies information from d to data
n->next = head;
if (head!=nullptr)
head->prev = n;
if(head == nullptr){
head = n;
tail = head;
} else
head = n;
}
尝试在应用程序验证器下运行它 - 发生损坏时可能会遇到断点。 – Pete 2013-02-28 10:09:15