HEAP CORRUPTION
为什么在执行delTree()最后释放tree时会出现heap corruption?
源程序:
tree.h
tree.cpp
源程序:
tree.h
#ifndef TREE_H_
#define TREE_H_
/* Definition of ITEM */
typedef int ITEM;
/* Definition of node */
struct node {
node *left;
node *right;
ITEM item;
};
/* Definition of TREE */
typedef struct {
node *root;
unsigned int numberOfNode;
}*TREE;
/* Miscellaneous Method */
TREE initTree();
bool addElement(TREE tree,ITEM item);
void delElement(TREE tree,ITEM item);
void delTree(TREE tree);
static void delNode(node *node);
static void copyToNode(node *ptr,ITEM item);
static node * findPosition(TREE tree, //At here,the value 0 of direction presents left
ITEM item, //and the value 1 of direction presents right
node **prev, //prev is designed for further use.
bool &direction,
int (*compare)(ITEM item1,ITEM item2)); //Compare function is designed by user.
static int compare(ITEM item1,ITEM item2);
#endiftree.cpp
#include <iostream>
#include "tree.h"
using namespace std;
TREE initTree()
{
TREE tree;
if(!(tree=(TREE)malloc(sizeof(TREE))))
return NULL;
tree->root=NULL;
tree->numberOfNode=0;
return tree;
}
bool addElement(TREE tree,ITEM item)
{
node *ptr;
if(!(ptr=(node *)malloc(sizeof(node))))
return false;
copyToNode(ptr,item);
ptr->left=ptr->right=NULL;
if(!tree->root)
tree->root=ptr;
else
{
bool direction=0;
node *prev=NULL;
if(findPosition(tree,item,&prev,direction,compare))
return false;
if(direction)
prev->right=ptr;
else
prev->left=ptr;
}
tree->numberOfNode++;
return true;
}
void delElement(TREE tree,ITEM item)
{
node *ptr=NULL,*prev=NULL;
bool direction=false;
if(ptr=findPosition(tree,item,&prev,direction,compare))
{
if(!ptr->left&&!ptr->right)
{
free(ptr);
if(direction)
prev->right=NULL;
else
prev->left=NULL;
}
else if(ptr->left&&!ptr->right)
{
if(direction)
prev->right=ptr->left;
else
prev->left=ptr->left;
free(ptr);
}
else if(!ptr->left&&ptr->right)
{
if(direction)
prev->right=ptr->right;
else
prev->left=ptr->left;
}
else
{
if(direction)
{
prev->right=ptr->left;
node *cur=ptr->left;
while(cur->right)
cur=cur->right;
cur->right=ptr->right;
free(ptr);
}
else
{
prev->left=ptr->left;
node *cur=ptr->left;
while(cur->right)
cur=cur->right;
cur->right=ptr->right;
free(ptr);
}
}
tree->numberOfNode--;
}
}
void delTree(TREE tree)
{
if(tree->root)
delNode(tree->root);
free(tree);
}
static void delNode(node *root)
{
if(root->left)
{
delNode(root->left);
root->left=NULL;
}
if(root->right)
{
delNode(root->right);
root->right=NULL;
}
free(root);
}
static void copyToNode(node *ptr,ITEM item)
{
ptr->item=item;
}
static node * findPosition(TREE tree,ITEM item,node **prev,bool &direction,int (*compare)(ITEM item1,ITEM item2))
{
node *cur=tree->root;
*prev=tree->root;
while(cur)
{
if(item==cur->item)
return cur;
else if(compare(item,cur->item)<0)
{
direction=false;
*prev=cur;
cur=cur->left;
}
else
{
direction=true;
*prev=cur;
cur=cur->right;
}
}
return NULL;
}
static int compare(ITEM item1,ITEM item2)
{
return item1-item2;
}
...全文
相关推荐
解决报错heap corruption detected after normal block.zip
HEAP CORRUPTION DETECTED"错误通常在程序退出之前触发,通过调试器可以查看调用栈,定位到导致问题的具体代码行。5.
Debugging Heap Corruption in Visual C++
Debugging Heap Corruption in Visual C++Debugging Heap Corruption in Visual C++ 是一篇关于使用 Microsoft Debugging
Heap Corruption(解决方案).md
Heap Corruption(解决方案).md
ERR_HEAP_CORRUPTION(解决方案).md
ERR_HEAP_CORRUPTION是指当程序运行时,在内存堆栈区域中出现了不可预知的错误,导致程序无法正常运行。
Understanding the Low Fragmentation heap(LFH)
- **Heap Corruption**:堆腐败,指的是堆中的数据结构被意外或恶意修改的情况,可能导致程序崩溃或安全漏洞。


