double pointer 2d array assigning problem

This looks way too stupid to be posted in beginners too :-/
This is my 1st post so pardon me for any mistake i make in creating posts :)

I wanted to create a double pointer so that i can hold a 2d array in game.
so i did this in the class declaration (header part)

1
2
3
4
5
6
7
class AIPathFinding{
   .
   .
   int **clearanceValues;
   .
   .
}


and in implementation

1
2
3
4
5
6
7
8
9
// in constructor
clearanceValues = new int*[width + 1];

for( int i = 0 ; i < width ; i++ ){
   clearanceValues[i] = new int[height + 1];
   clearanceValues[i][height] = NULL;
}

clearanceValues[width] = NULL;


This array depends on the map size so it has to be dynamic. I read some where that there is nothing like .length for an array to get the length of the array unlike in java. The only way is to put a NULL at the end of array and iterate through the array till you hit NULL. So initialized the array with length + 1 and initialized the final variable in array to NULL.

Now while generating clearance values for path finding i needed to increment the values in the array if certain condition is met.

1
2
3
4
5
6
7
8
9
10
for(int y = 0 ; y < height ; y++){
   for(int x = 0 ; x < width ; x++){
      clearanceValues[x][y] = 1;
      while( !breakWhile ){
	 if(ifCondition){
	    clearanceValues[x][y]++;
         }
      }
   }
}


but when i print the clearanceValues all the values are 2 (where ever ifCondition is true, you can see that it is in while so the clearanceValues[x][y] should be equal to number of times whileCondition && ifCondition is true).

I kept break point at line clearanceValues[x][y]++ to check how many times its is being hit. This line is being hit 4 times when x = 0, y = 0.

clearanceValues[x][y]++ is being called 4 times still its value is 2. Everytime checkpoint hits i check the value it says 2 only.

if i replace clearanceValues[x][y]++ with clearanceValues[x][y] = 4 then all values are becoming 4.

so i replaced clearanceValues[x][y]++ with clearanceValues[x][y] = clearanceValues[x][y] + 1 and also int tmp = clearanceValues[x][y] + 1;clearanceValues[x][y] = tmp; both gave same result.

no matter how many times this line is being hit it still says 2.

From this what i understood is if i assign numeric values directly (like clearanceValues[x][y] = 5 they are being assigned perfectly but when i try doing it with variables or uniary operators they are not being assigned)

I'm a java guy not much familiar with pointers and all. I know i'm missing something small like '*' or '&' or something. please help me understand the problem i'm facing here.
Maybe you did something wrong in the ifCondition and breakWhile? Did you use = instead of ==?
Hmm..., it's working for me, unless I misunderstood the problem. I just threw your code together and added a bit to see see the output:

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
#include <iostream>

int main()
{
    int width = 5, height = 3, numLoops = 4;

    int **clearanceValues;
    
    clearanceValues = new int*[width + 1];

    for ( int i = 0 ; i < width ; i++ ) {
       clearanceValues[i] = new int[height + 1];
       clearanceValues[i][height] = NULL;
    }

    clearanceValues[width] = NULL;
    
    for (int y = 0 ; y < height ; y++) {
        for (int x = 0 ; x < width ; x++) {
            bool breakWhile = false;
            clearanceValues[x][y] = 1;
          
            while (!breakWhile) {
	        if (clearanceValues[x][y] < numLoops) {
	            clearanceValues[x][y]++;
                }
                else {
                    breakWhile = true;
                }
            }
            
            std::cout << clearanceValues[x][y];
        }
        
        std::cout << '\n';
    }
    
    for ( int i = 0 ; i < width ; i++ ) {
       delete clearanceValues[i];
    }
    
    delete clearanceValues;
    
    return 0;
}


Every element in clearanceValues ends up equal to numLoops, which is what I thought was the issue. Did I misunderstand the problem?
Last edited on
This is the full code i'm using.
No i'm not using '==' instead of '=' in fact the clearanceValue array is never used to check for any conditions.

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
// generating the clearance matrix
for(int y = 0 ; y < mapSize.height ; y++){
   for(int x = 0 ; x < mapSize.width ; x++){
	
      int offset = 0;
      bool breakWhile = false;

      while( !breakWhile ){
         // if the area after adding offset is inside map
         if( (x + offset < mapSize.width) && (y + offset < mapSize.height) ){
            // check if the tile in current position (x, y) is moveable
            // set the value to 1 else 0
            if( CommonData::IsTankMoveableTile(ly->tileGIDAt(x, y)) ){
               tank->tankAI->clearanceValues[x][y] = 1;
            }
            else{
               tank->tankAI->clearanceValues[x][y] = 0;
               break;
            }
				
            // checking all the tiles that are there on bottom side
            // if any tile is not moveable by tank break the loop
            for(int offsetx = x ; offsetx <= x + offset ; offsetx++){
               if( !CommonData::IsTankMoveableTile(ly->tileGIDAt(offsetx, y + offset)) ){
                  breakWhile = true;
                  break;
               }
            }
            if(breakWhile) break;
				
            // checking all the tiles that are there on right side
            // if any tile is not moveable by tank break the loop
            for(int offsety = y ; offsety <= y + offset ; offsety++){
               if( !CommonData::IsTankMoveableTile(ly->tileGIDAt(x + offset, offsety)) ){
                  breakWhile = true;
                  break;
               }
            }
            if(breakWhile) break;

            // if loop didnt break till here then that means that all tiles that are present 
            // inside the offset value are moveable increment offset to check for next level
            offset++;
            // incrememting the clearance value for the tile at (x, y)
            tank->tankAI->clearanceValues[x][y]++;

            // debug purpose printing the values for each loop
            sprintf(stringtank, "(%d, %d) : %d\n", x, y, tank->tankAI->clearanceValues[x][y]);
            OutputDebugString((LPCSTR)&stringtank[0]);
				
         } // end of if for checking if the offset co-ordinates are inside the map if not break the while
         else{
            break;
         }

      }
   }
}


for those who are curious about algorithm i used, please take a look at this link. Its a bit complicated to explain here.

http://aigamedev.com/open/tutorial/clearance-based-pathfinding/

This is the code i used to generate the matrix. Exact code.

@monad thanks for the sample code really appreciate it. I even tried it and it worked like charm. But as you can see i'm doing exactly same in my code too.


there is no code in entire project that is messing with the array currently. I just started writing this part of AI. The output i got looks like this.

My map size is 38x30

(0, 0) : 2
(0, 0) : 2
(0, 0) : 2
(0, 0) : 2
(1, 0) : 2
(1, 0) : 2
(1, 0) : 2
(2, 0) : 2
.
.
.
(36, 29) : 2
(37, 29) : 2


As you can see every value is 2. From 1st to last in every iteration the value remains as 2.
And to make thing worst, when i tried to output all the values in clearanceValue at once by using this code just after the above code

1
2
3
4
5
6
7
for(int y = 0 ; y < mapSize.height ; y++){
   for(int x = 0 ; x < ly->mapSize.width ; x++){
      sprintf(stringTmp, "%d ", tank->tankAI->clearanceValues[x][y]);
      OutputDebugString((LPCSTR)&stringTm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 0 1 1 0 0 1 1 1 0 1 1 0 1 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 
2 2 1 1 1 1 1 2 2 2 2 2 2 2 2 1 1 0 1 1 0 2 2 2 2 2 2 2 2 2 1 1 1 1 1 2 2 2 
2 2 2 1 0 0 0 2 2 2 2 2 2 2 2 2 1 0 2 1 0 2 2 2 2 2 2 2 2 2 2 1 0 0 0 2 2 2 
2 2 2 2 0 0 0 2 2 2 2 2 2 2 2 2 2 0 2 2 0 2 2 2 2 2 2 2 2 2 2 2 0 0 0 2 2 2 


Thanks for your time :)
Thanks to visual studio breaking points and a frnds help i got it.

at line 13-20 the values are being re initialized to 1 in every iteration of while.
so i'm getting 2 at the end of every iteration.

cant believe that i made such a small mistake.
Thank everyone
Topic archived. No new replies allowed.