0

我创建了一个应该删除数组的第一个元素的方法,但是当我运行我的代码时,调试器会翻转,我不知道为什么。

这是我的 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
};
4

2 回答 2

1

尝试delete [] temp;在 for 循环下移动。

看起来一个问题可能是delete [] temp;在 for 循环中被重复调用。通过循环的第一次迭代,与之相关的内存temp将被释放。通过循环的后续迭代将访问释放的内存。

可能还有其他问题。查看调试器的输出会很有帮助。

于 2013-10-06T21:48:38.777 回答
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;
}

我强烈建议ListOfLoans用 STL 容器替换,例如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;
}
于 2013-10-06T22:01:11.913 回答