2012-10-24 34 views
1

我已經寫了一個avl的代碼,它在執行過程中執行/ mid方式後給了我下面的錯誤。
我想它是某種內存泄漏問題。有人可以指出應該修正什麼? Codeblocks給出了一個彈出式分割錯誤。 錯誤:Unhandled exception at 0x77B2A710 (ntdll.dll) in ADS Project.exe: 0xC0000005: Access violation writing location 0x00000014.AVL樹的C++代碼給出訪問衝突錯誤

代碼:

{ 
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; 
} 
+1

您是否嘗試過使用調試器? – jogojapan

+0

它只是讓我chkstk.asm我是新來的c + +所以不能讓大部分的文件,我真的不能進入代碼 – Shekhar

+0

所以你使用的調試器?在這種情況下,請在發生錯誤時輸出調用堆棧的內容。這應該告訴你代碼中的哪一行導致了直接的錯誤。 (實際的問題可能在其他地方,但它仍然有助於找出發生了什麼問題。) – jogojapan

回答

3

一個問題,我可以看到的是:

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

應該

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

可能會有更多。調試器是您在這種情況下最好的朋友。

+0

謝謝:)它的工作爲10000 – Shekhar

+0

我仍然無法讓它工作的1,000,000個數字,它只是立即退出ADS Project.exe 0x003228F7錯誤未處理的異常:0xC00000FD:堆棧溢出(參數:0x00000000,0x01002000 )。 – Shekhar

+0

發現了這個問題,達到了最大數組大小 – Shekhar