2013-10-06 22 views
0

我已經創建了一個方法,應該刪除數組的第一個元素,但是當我運行我的代碼時,調試器翻轉出來,我不知道爲什麼。數組,removeFirst()不工作?

這是我的removeFirst()方法:

Loan & ListOfLoans :: removeFirst(){ 
    index = 0; 
    //determine if the container needs to be shrunk 
    if((numberOfElements < capacity/4) && (capacity >= 4)){ // shrink container when  array is 1/4 full 
     cout<<"shrinking array! \n"; 
     Loan ** temp = elements; 
     elements = new Loan * [numberOfElements/2]; 
     //copy temp array to elements 
     for(int i = 0; i<numberOfElements; i++){ 
      temp[i] = elements[i]; 
      numberOfElements = numberOfElements/2; 
      delete [] temp; 
     } 
    } 
    numberOfElements--; 
    return **elements; 
} 

而且好措施我的頭文件:

#include <iostream> 
#include "loan.h" 

using namespace std; 

class ListOfLoans { 

public: 
ListOfLoans(int initial_size=4); 

~ListOfLoans(void); 


void add(Loan & aLoan); 
Loan & first() ; 
Loan & removeFirst(); 
    // answer the first element from the list and remove it from the list 
    // if the resulting list is more than three quarters empty release some memory 
Loan & removeLast(); 
    // answer the last element from the list and remove it from the list 
    // if the resulting list is more than three quarters empty release some memory 
Loan & next(); 
int size(); 

private: 
Loan ** elements; //actuall stuff in the array m_pnData; 
int numberOfElements; //number of elements in the list stize of the array? m_nLength 
int capacity; //size of the available array memory 
int index; //used to help with the iteration 
}; 

回答

0

此代碼有許多問題。這是一個帶註釋的副本。

// Return by reference cannot be used if you are removing the element. 
// This looks like a Java-ism. 
Loan & ListOfLoans :: removeFirst(){ 
    // unused variable 
    index = 0; 

    if((numberOfElements < capacity/4) && (capacity >= 4)){ 
     cout<<"shrinking array! \n"; 
     Loan ** temp = elements; 
     // you are allocating an array of size numberOfElements/2... 
     elements = new Loan * [numberOfElements/2]; 

     // then accessing elements which are way past its end. 
     for(int i = 0; i<numberOfElements; i++){ 
      temp[i] = elements[i]; 
      // you are halving the array size in every iteration 
      numberOfElements = numberOfElements/2; 
      // you are deleting temp in every iteration, 
      // causing double-frees 
      delete [] temp; 
     } 
    } 
    // if the array does not need to be shrunk, 
    // you are actually removing the last element. 
    // the removed element is not freed and is leaking. 
    numberOfElements--; 
    // you are returning the first element, 
    // which is not removed. 
    return **elements; 
} 

我會強烈建議使用STL容器更換ListOfLoans,如std::deque。如果你不能這樣做,這是一個最小的固定版本。

Loan ListOfLoans::removeFirst() { 
    Loan to_return; 
    if (numberOfElements == 0) { 
     return to_return; // return default value, there is nothing to remove 
    } 
    if ((numberOfElements < capacity/4) && (capacity >= 4)) { 
     Loan **old = elements; 
     capacity = capacity/2; 
     elements = new Loan*[capacity]; 
     for (int i=0; i < numberOfElements - 1; ++i) { 
      elements[i] = old[i+1]; 
     } 
     to_return = *old[0]; 
     delete old[0]; 
     delete[] old; 
    } else { 
     to_return = *elements[0]; 
     delete elements[0]; 
     for (int i=0; i < numberOfElements - 1; ++i) { 
      elements[i] = elements[i+1]; 
     } 
    } 
    --numberOfElements; 
    return to_return; 
} 
+0

我真的很陌生,所以請耐心等待我...我無法使用貸款聲明to_return,因爲沒有適當的默認構造函數...我認爲我的構造函數要求我發起每個新貸款5參數,我不能有兩個構造函數,因爲我已經給了頭文件並且不能改變它 – Sarah

+0

在這種情況下調查'boost :: ptr_deque'或'boost :: ptr_list'。這些是標準容器的包裝,允許他們使用指針。 –

1

嘗試下移動delete [] temp; for循環。

看起來像一個問題可能是delete [] temp;在for循環中被重複調用。 通過循環的第一次迭代,與temp關聯的內存將被釋放。循環的後續迭代將訪問釋放的內存。

可能還有其他問題。看到調試器的輸出會非常有幫助。

+0

良好的通話刪除。不幸的是,這不是問題。我真的不知道調試器發生了什麼(我從來沒有用過),但我正在運行Microsoft Visual Studio中的代碼,並且一個iosfwd文件不斷彈出。 – Sarah