我正在尝试释放动态内存但我收到了错误。我需要一些帮助来指出我的代码的哪一部分导致错误。
我有析构函数来释放两个动态数组。删除析构函数我没有错误。
错误:_BLOCK_TYPE_IS_VALID(pHEAD-> nBlockUse)
分数等级:
class Scores
{
private:
int numOfTest;
double *scoresArray = nullptr;
double scoreAvg;
double *scoreSort = nullptr;
public:
Scores(int);
~Scores();
void setNumOfTest();
int getNumOFTest();
void setScores();
double* getScores();
void setSort();
double* getSort();
void setScoreAvg();
double getScoreAvg();
};
Scores::Scores(int num)
{
scoresArray = new double [num];
scoreSort = new double[num];
numOfTest = num;
}
Scores::~Scores()
{
delete [] scoresArray;
delete [] scoreSort;
scoresArray = nullptr;
scoreSort = nullptr;
}
void Scores::setNumOfTest()
{
// Verify number of test scores is positive
while (numOfTest <= 0)
{
cout << "The number of test need to be postive." << endl;
cout << "Enter another number: ";
cin >> numOfTest;
}
}
int Scores::getNumOFTest()
{
return numOfTest;
}
void Scores::setScores()
{
double scores;
int size = getNumOFTest();
//scoresArray = new double[size];
for (int i = 0; i < numOfTest; i++)
{
cout << "Enter test score " << i + 1 << ": ";
cin >> scores;
// Verify if score enter is positive
while (scores < 0)
{
cout << "Negative scores are not allowed." << endl;
cout << "Enter another score for this test: ";
cin >> scores;
}
scoresArray[i] = scores;
}
}
double* Scores::getScores()
{
//double *sa = scoresArray;
//return sa;
return scoresArray;
}
void Scores::setSort()
{
int size = getNumOFTest();
//scoreSort = new double[size];
scoreSort = getScores();
for (int i = 0; i < size; i++)
{
int smallPos = i;
double smallest = scoreSort[smallPos];
for (int j = i + 1; j < size; j++)
{
if (scoreSort[j] < scoreSort[smallPos])
{
smallPos = j;
smallest = scoreSort[smallPos];
}
}
scoreSort[smallPos] = scoreSort[i];
scoreSort[i] = smallest;
}
}
double* Scores::getSort()
{
//double *ss = scoreSort;
//return ss;
return scoreSort;
}
void Scores::setScoreAvg()
{
int size = getNumOFTest();
for (int i = 0; i < size; i++)
{
scoreAvg += scoresArray[i];
}
scoreAvg /= size;
}
double Scores::getScoreAvg()
{
return scoreAvg;
}
主:
int main()
{
int numOfTest;
cout << "How many test scores will you enter" << endl;
cin >> numOfTest;
Scores s(numOfTest);
s.setNumOfTest();
s.setScores();
s.setSort();
s.setScoreAvg();
double y = s.getScoreAvg();
double *x = nullptr;
x = new double[numOfTest];
x = s.getSort();
cout << "sort" << endl;
for (int i = 0; i < numOfTest; i++)
{
cout << x[i] << "\n";
}
cout << "avg" << endl;
cout << y;
delete[] x;
x = nullptr;
//s.Scores1();
return 0;
}
答案 0 :(得分:1)
您看到的是双重删除。一个明显的问题是,您在这里获得了一个指向对象s
的动态分配数组scoresArray
的指针:
x = s.getSort(); // also leaks memory previously pointed at by x
然后你打电话给delete[]
:
delete[] x; // calls delete[] on s.scoresArray
但s
对象也在其析构函数中调用delete[]
。这会导致双重删除。
除此之外,代码中可能还有其他错误。
请注意,您可以通过不显式使用动态分配来避免此问题,而是依赖std::vector<double>
之类的类型。
答案 1 :(得分:0)
问题在于Scores::setSort
:
scoreSort = getScores();
getScores()
返回scoresArray
。所以现在scoreSort
和scoresArray
指向同一个动态分配的块。当您尝试删除它们时,您将两次删除相同的块。
您应该将scoresArray
的内容复制到scoreSort
,然后对scoreSort
进行排序,而不是分配指针。