Cant delete object

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
int NextPixel(int antIndex, int currentPixel , Ant *ant , IplImage *edgeImg)
{    
	int left = currentPixel - 1 ;
	int right = currentPixel + 1 ;
	int up = currentPixel - edgeImg->widthStep ;
	int down = currentPixel + edgeImg->widthStep ;
	int leftDown = currentPixel + edgeImg->widthStep - 1;
	int rightDown = currentPixel + edgeImg->widthStep + 1;
	int leftup = currentPixel - edgeImg->widthStep - 1;
	int rightup = currentPixel - edgeImg->widthStep + 1;
	int probArr[] = {left,right ,up ,down, leftDown ,rightDown ,leftup,rightup};

	ProbWays *probs = MoveProbs(antIndex, currentPixel, ant ,edgeImg , probArr);

	double *cumul = new double[probs->length + 1];
	memset(cumul , 0 , sizeof(double) * probs->length + 1);

    for (int i = 0; i < probs->length ; ++i)
		cumul[i + 1] = cumul[i] + probs->probs[i]; // consider setting cumul[cuml.Length-1] to 1.00

    double p = ((double) rand() / (RAND_MAX)) ;
	int nextPixel ;

    for (int i = 0; i < probs->length ; ++i) 
	{
		if (p >= cumul[i] && p < cumul[i + 1]) { nextPixel = probArr[i]; break ; }
	} 
	
	delete cumul;
	//delete probs->probs ;
        //delete probs ;  
	return nextPixel;
}


ProbWays *MoveProbs(int antIndex, int currentPixel , Ant *ant , IplImage *edgeImg , int *probArr)
{
	ProbWays *ways = new ProbWays[];
	double *taueta = new double[PROBSBELENGTH];
	int count = 0 ;
    double sum = 0.0; // sum of all tauetas
    for (int i = 0; i < PROBSBELENGTH; ++i) 
    {
		if (probArr[i] > ant->pheromoneLength || probArr[i] < 0)
		{
			break ;
		}
	    if (checkItemExist(ant->visited,probArr[i],tabu)) // check if visited pixel in trail
            taueta[i] = 0.0; // prob of moving to a visited city is 0

        else
        {
			taueta[i] = pow(ant->pheremones[probArr[i]], ALPHA) * pow((1.0 / dist(currentPixel, probArr[i],edgeImg)), BETA); 
            if (taueta[i] < 0.0001)
                taueta[i] = 0.0001;
        }
		count++;
        sum += taueta[i];
    }
	
	ways->probs = new double[count];
	ways->length = count ;

    for (int i = 0; i < ways->length ; ++i)
	{
        ways->probs[i] = taueta[i] / sum; // big trouble if sum = 0.0
	}
	delete taueta;
    return ways;
}

typedef struct ProbWays
{
	double *probs ;
	int length ;
}ProbWays;



delete probs->probs ;
delete probs ;

if those lines are uncommented , program gives heap error.Why , any idea ?
Last edited on
And where exactly does that error occur? There isn't really enough information to really diagnose your problem. For instance, what does MoveProbs do? And what happens in the destructor for ProbWays?

-Albatross
> ProbWays *ways = new ProbWays[];
that statement is illegal, consult your compiler documentation to know what is doing.

Also read http://www.parashift.com/c++-faq/allocate-array.html and http://www.parashift.com/c++-faq/delete-array.html


Consider using a container like std::vector or std::valarray
Ty for your helping appreciated...
Topic archived. No new replies allowed.