Apr 17, 2014 at 2:04pm UTC
Hello , I just wonder how I can delete a parameter in a function .
As below ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int *buildTrail(int antIndex, int start, double *pheromones)
{
int *trail = new int [tabu];
bool *visited = new bool [tabu];
trail[0] = start;
visited[start] = true ;
for (int i = 0; i < tabu - 1; ++i)
{
int currentPixel = trail[i];
int nextPixel = ++currentPixel /*= NextCity(antIndex, currentPixel, visited, pheromones)*/ ;
trail[i + 1] = nextPixel;
visited[nextPixel] = true ;
}
return trail;
}
If i comment all lines includes visited word , no exception occurs ,
Otherwise , exception throws.
Simply put , How can i delete visited parameter as long as its role has been finished?
1 2 3 4 5
.
.
.
delete visited ;
return trail;
Last edited on Apr 17, 2014 at 2:09pm UTC
Apr 17, 2014 at 2:09pm UTC
delete [] visited;
Be sure that this statement is placed after all possible accesses to the array. Alternatively, use vectors.
Apr 17, 2014 at 2:15pm UTC
I tried but does not work.
I write delete[] before "return trail".
Apr 17, 2014 at 2:20pm UTC
"delete [] visited;" should work.
Apr 17, 2014 at 2:20pm UTC
1 2 3 4 5 6 7
std::vector<int >
buildTrail(int antIndex, int start, double *pheromones){
std::vector<int > trail( tabu );
std::vector<bool > visited( tabu );
//...
return trail;
}
> If i comment all lines includes visited word , no exception occurs ,
> Otherwise , exception throws.
unless it is throwing `std::bad_alloc' after you run out of memory, a memory leak would not cause an exception to be thrown.
delete
is different than
delete []
new
is different than
new []
Last edited on Apr 17, 2014 at 2:21pm UTC