我创建了一个方法,应该删除数组的第一个元素,但是当我运行我的代码时,调试器会翻转,我不知道为什么。
这是我的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 :(得分:1)
尝试在for循环下移动delete [] temp;
。
看起来一个问题可能是在for循环中重复调用delete [] temp;
。
通过循环的第一次迭代,与temp
相关联的内存将被释放。通过循环的后续迭代将访问释放的内存。
可能还有其他问题。查看调试器的输出非常有用。
答案 1 :(得分: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;
}