我已经为AVL编写了一个代码,该代码在执行后在执行/中期后给我以下错误。
我猜这是某种内存泄漏问题。有人可以指出应该解决什么吗?CodeBlocks给出了弹出分段错误。错误:Unhandled exception at 0x77B2A710 (ntdll.dll) in ADS Project.exe: 0xC0000005: Access violation writing location 0x00000014.
代码:
{
InputGenerator ip;
int numbers[1000000];
ip.RandomInput(numbers, 1000000);
AVL *avlTree = (AVL *) malloc(1000000* sizeof(AVL *));
AVL *root = NULL;
auto avlOps = make_shared < AVL > ();
int input = 0;
*avlTree = AVL(numbers[0]);
root = avlTree;
avlTree++;
int balanceFac = 0;
input = 1;
while (input != 1000000)
{
//cout << "sorting : ";
//avlOps->InorderPrint(root);
cout << endl;
cout << "Inserting : " << numbers[input] << endl;
*avlTree = AVL(numbers[input]);
avlOps->Insert(avlTree, root);
// check if rotation is required.
AVL * tempNo=avlTree->GetParent();
while(tempNo!=NULL)
{
int balFac=0;
AVL* node1=NULL;
AVL* node2=NULL;
AVL* node3=NULL;
int rCase=0;
balFac=avlOps->GetBalanceFactor(tempNo);
if(balFac>1||balFac<-1)
{
node1=tempNo;
if(balFac>0)
{
node2=node1->GetLChild();
balFac=avlOps->GetBalanceFactor(node2);
if(balFac>0)
{
node3=node2->GetLChild();
rCase=1;
}
else
{
node3=node2->GetRChild();
rCase=3;
}
}
else
{
node2=node1->GetRChild();
balFac=avlOps->GetBalanceFactor(node2);
if(balFac>0)
{
node3=node2->GetLChild();
rCase=4;
}
else
{
node3=node2->GetRChild();
rCase=2;
}
}
root=avlOps->Rotation(node1,node2,node3,root,rCase);
}
tempNo=tempNo->GetParent();
}
cout<<endl;
cout << "Root :" << root->GetKey() << endl;
cout << "******" << endl;
avlTree++;
input++;
}
avlOps->InorderPrint(root);
return 0;
}
我可以看到的一个问题是:
AVL *avlTree = (AVL *) malloc(1000000* sizeof(AVL *));
应该是
AVL *avlTree = (AVL *) malloc(1000000* sizeof(AVL));
^^^
可能还有更多。在这种情况下,调试器是您最好的朋友。