gpt4 book ai didi

c++ - 可用动态数组内存错误 : _BLOCK_TYPE_IS_VALID

转载 作者:行者123 更新时间:2023-11-28 06:25:57 26 4
gpt4 key购买 nike

我正在尝试释放动态内存,但出现错误。我需要一些帮助来指出我的代码的哪一部分导致了错误。

我有析构函数来释放两个动态数组。删除析构函数我没有错误。

错误:_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;
}

最佳答案

您看到的是双重删除。一个明显的问题是您得到一个指向对象 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> 等类型来轻松避免此问题。相反。

关于c++ - 可用动态数组内存错误 : _BLOCK_TYPE_IS_VALID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28524679/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com