Increasing Efficiency

Hello, I'm working on an AI for Starcraft 1. I'm implementing a two dimensional array which, on an average sized map, will be 128 by 128. A few operations are performed on each square (16384). My problem is that the function crashes roughly half way through. Here is the code:

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
double **tPFMap;

void buildTPFMap() {

	int mapHeight = Broodwar->mapHeight();
	int mapWidth = Broodwar->mapWidth();
	int percentDone = 0;

	// Constructing tPFMap
	tPFMap = new double*[mapWidth];
	for(int i = 0; i < mapWidth; i++) {
		tPFMap[i] = new double[mapHeight];
	}

	TilePosition currentTile;
	Position currentPos;

	// Goal is located 1/4 in from X = 0 and Y = 0 from the top left.
	TilePosition goalTile = TilePosition(mapWidth/4, mapHeight/4);

	// Note that point 0,0 is the top left
	for(int i = 0; i < mapWidth; i++) {
		for(int j = 0; j < mapHeight; j++) {

			currentTile = TilePosition(i, j);
			currentPos = Position((i * 32) + 16, (j * 32) + 16);

			TilePosition nearest = TilePosition(BWTA::getNearestUnwalkablePosition(currentPos));

			// Sets a positive charge for each tile relative to its distance to the goal.
			tPFMap[i][j] += (10/BWTA::getGroundDistance(goalTile, currentTile));

			// Sets a negative charge for tiles near unwalkable areas 
			if(BWTA::getGroundDistance(nearest, currentTile) <= 48) {

				Broodwar->drawDot(CoordinateType::Map, currentPos.x(), currentPos.y(), Colors::Red);
				tPFMap[i][j] -= 10;
			}
		}
		printf("%d out of %d done\n", i, mapWidth);
	}
}


This code will only be called once. Obviously I don't expect you to know the Starcraft related methods and I cant make them any more efficient anyways. I simply need to know if I can make use of the array more efficiently. Thanks for any help!
Run it in a debugger - which line does it fail at and what is the error?

One thing to watch out for is, Line 12 is insufficient for initializing POD (plain-old-data).
In C++, new[] called on an Object will call the default constructor, but new[] called on POD does nothing to initialize.
First off, when I attempt to debug, it says:

Debugging information for "ExampleAIClient.exe" cannot be found or does not match. Binary was not built with debug information.

If i continue and run my program, when I run the method shown above I get:

Unhandled exception at 0x00261871 in ExampleAIClient.exe: 0xC0000005: Access violation reading location 0xababac2b.
You need to turn on debugging (-g for gcc, but I don't know what compiler you are using).

Otherwise, I recommend printing tPFMap[i][j] before AND after Line 31.
You should also print mapHeight, mapWidth, and tPFMap (should be a non-null pointer) as a sanity check.

I suspect that you will get garbage due to no initialization.

One other note - since you have a rectangular matrix, I would actually just allocate

 
double *tPFMap =  new double[mapWidth * mapHeight];


You have less fragmentation of memory this way (but you will have to update your code a little).
Topic archived. No new replies allowed.