1

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;
}
4

1 に答える 1

3

私が見ることができる1つの問題は次のとおりです。

AVL *avlTree = (AVL *) malloc(1000000* sizeof(AVL *));

する必要があります

AVL *avlTree = (AVL *) malloc(1000000* sizeof(AVL));
                                              ^^^

もっとあるかもしれません。このような状況では、デバッガーはあなたの親友です。

于 2012-10-24T02:26:36.853 に答える