I've got a section of a program that I can't quite get to work. I'm fairly sure its something very simple/trivial but it looks correct to me, so if someone could help me fix the problem, and explain what it is that is wrong, that would be great... I've posted a sample of code, which is the bit of interest:
[CODE=cpp]class Solution
{
private:
int *HeuristicTours ;
public:
int* GetTours()
{
return HeuristicTours;
}
};[/CODE]
[CODE=cpp]int run(Problem &p)
{
int **m_ppiTourMatr ix_bestsofar; // declared globally so cleanup() can access
int *piNext, *piTours;
...
piNext = p.solution.GetT ours();
for (i=0; i<p.solution.He uristicVehicleC ount; i++)
{
iCount = m_ppiTourMatrix _bestsofar[i][0];
for (j=1; j<=iCount; j++)
{
*piNext = m_ppiTourMatrix _bestsofar[i][j];
piNext++;
}
*piNext = -1;
piNext++;
}
cleanup();
return 0;
}
void cleanup()
if (m_ppiTourMatri x_bestsofar != NULL)
{
free(m_ppiTourM atrix_bestsofar );
m_ppiTourMatrix _bestsofar = NULL;
}
[/CODE]
m_ppiTourMatrix _bestsofar is populated with values to start with, so the aim is to copy these values to the p.solution tours. This then can iterate through the tours later, using -1 as a termination of a tour.
The problem I have is that although the values are set fine before calling cleanup(), as soon as cleanup() has been called my values within my solution tours are now uninitalised.
I thought that [CODE=cpp]*piNext = m_ppiTourMatrix _bestsofar[i][j];[/CODE] would copy the value within the matrix, into the value stored at location piNext, but if that were the case then freeing up m_ppiTourMatrix _bestsofar shouldn't make any difference should it?
Thanks for your help.
[CODE=cpp]class Solution
{
private:
int *HeuristicTours ;
public:
int* GetTours()
{
return HeuristicTours;
}
};[/CODE]
[CODE=cpp]int run(Problem &p)
{
int **m_ppiTourMatr ix_bestsofar; // declared globally so cleanup() can access
int *piNext, *piTours;
...
piNext = p.solution.GetT ours();
for (i=0; i<p.solution.He uristicVehicleC ount; i++)
{
iCount = m_ppiTourMatrix _bestsofar[i][0];
for (j=1; j<=iCount; j++)
{
*piNext = m_ppiTourMatrix _bestsofar[i][j];
piNext++;
}
*piNext = -1;
piNext++;
}
cleanup();
return 0;
}
void cleanup()
if (m_ppiTourMatri x_bestsofar != NULL)
{
free(m_ppiTourM atrix_bestsofar );
m_ppiTourMatrix _bestsofar = NULL;
}
[/CODE]
m_ppiTourMatrix _bestsofar is populated with values to start with, so the aim is to copy these values to the p.solution tours. This then can iterate through the tours later, using -1 as a termination of a tour.
The problem I have is that although the values are set fine before calling cleanup(), as soon as cleanup() has been called my values within my solution tours are now uninitalised.
I thought that [CODE=cpp]*piNext = m_ppiTourMatrix _bestsofar[i][j];[/CODE] would copy the value within the matrix, into the value stored at location piNext, but if that were the case then freeing up m_ppiTourMatrix _bestsofar shouldn't make any difference should it?
Thanks for your help.
Comment