I was solving a simple math question which needs to add solutions continuouslly to the answer set. So I designed a class with a pointer to a pointer in it. Whenever a new solution is worked out, I define another new temporary pointer to a pointer to hold answer set's data and enlarge the capacity of answer set and re-initialize it. Then copy former data and store new solution to answer set.
While deleting the temporary pointer, the program always crashes into "Invalid address specified to RtlValidateHeap" error. Exactly at deleting the pointer arrays pointed by it.
Removing the corresponding codes makes everything work right. But I guess there will be a memory leak with larger data set because of missing delete[]
.
I think I had used copy function in a right way and made every unused pointer point to NULL.
So, here's my code:
#include <iostream>
#include "CChickProblem.h"
using namespace std;
int main(int argc, char const *argv[])
{
int n;
cin >> n;
while (n--)
{
int a, b;
cin >> a >> b;
CChickProblem c(a, b);
c.findSolution();
c.printSolution();
}
return 0;
}
CChickProblem.h:
#pragma once
class CChickProblem
{
private:
int cockNum, henNum, chickNum, answerNum, totalNum, totalPrice;
static int singleCockPrice, singleHenPrice, tripleChickPrice, chickenKindNum;
int **answer;
public:
CChickProblem(int money, int number);
~CChickProblem();
void findSolution();
void printSolution();
};
CChickProblem.cpp:
#include <iostream>
#include "CChickProblem.h"
using namespace std;
int CChickProblem::singleCockPrice = 5;
int CChickProblem::singleHenPrice = 3;
int CChickProblem::tripleChickPrice = 1;
int CChickProblem::chickenKindNum = 3;
CChickProblem::CChickProblem(int number, int money)
{
totalNum = number;
totalPrice = money;
cockNum = henNum = chickNum = answerNum = 0;
answer = nullptr;
}
CChickProblem::~CChickProblem()
{
for (int i = 0; i < answerNum; i )
{
delete[] * (answer i);
}
delete[] answer;
}
void CChickProblem::findSolution()
{
for (cockNum = 0; cockNum <= (totalPrice / singleCockPrice); cockNum )
{
for (henNum = 0; henNum <= (totalPrice / singleHenPrice); henNum )
{
for (chickNum = 0; chickNum <= (totalPrice / tripleChickPrice * 3); chickNum = 3)
{
if (cockNum * henNum * chickNum != 0 && cockNum henNum chickNum == totalNum && cockNum * singleCockPrice henNum * singleHenPrice chickNum / 3 * tripleChickPrice == totalPrice)
{
int **extra = new int *[answerNum];
for (int i = 0; i < answerNum; i )
{
*(extra i) = new int[chickenKindNum];
}
copy(answer, answer answerNum, extra);
answer = new int *[ answerNum];
for (int i = 0; i < answerNum; i )
{
*(answer i) = new int[chickenKindNum];
}
copy(extra, extra answerNum - 1, answer);
if (extra)
{
for (int i = 0; i < answerNum - 1; i )
{
if (extra[i])
{
delete[] extra[i];//Where crashes!!
extra[i] = nullptr;
}
}
delete[] extra;
extra = nullptr;
}
*(answer answerNum - 1) = new int[chickenKindNum];
for (int i = 0; i < chickenKindNum; i )
{
switch (i)
{
case 0:
*(*(answer answerNum - 1) i) = cockNum;
break;
case 1:
*(*(answer answerNum - 1) i) = henNum;
break;
case 2:
*(*(answer answerNum - 1) i) = chickNum;
break;
default:
break;
}
}
}
}
}
}
}
void CChickProblem::printSolution()
{
cout << answerNum << endl;
for (int i = 0; i < answerNum; i )
{
for (int j = 0; j < chickenKindNum; j )
{
cout << *(*(answer i) j);
if (j != 2)
{
cout << ' ';
}
else
{
cout << endl;
}
}
}
}
Please help me, thanks!
CodePudding user response:
Ok,guys. After days of testing and trying,I figured out where this foolish problem is. The delete [] extra[i]
in an attempt to delete the pointers is totally unneccessary cause it will remove the pointed data on the heap which is arranged to be reused in the following code without deleting the pointer itself. And, as it has removed data in the heap, when program ends at a destructer,it will find nothing to destruct, thus trigger error.
So the proper way to destruct pointers without deleting the pointed data, is just to create another pointer to the originally pointed data in case you can't find them and make the original pointer point to NULL.
In my program, it should be like deleting the pointer to a pointer after copy progress.