Jump to content
  • Advertisement
Sign in to follow this  
TalonSnow

Problem allocating enough memory[SOLVED]

This topic is 4725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok so i went to load in a bigger map to test out my movement code and map loader. However it has one problem, and thats allocating enough memory to read it the data. It was dynamically allocating what it needed once it found out how many objects were going to be stored. Once I made this into a number quite larger it worked fine. originally it would pull in a size of 20 (which logfile shows the results of) when i set it to 40 as shown in code also if pull sit all in fine. What am i missing here that is reducing the size of what i need?
//globals
struct mapHexTravel
{
	int destHexX;
	int destHexY;
	int movePoints;
};

struct mapHex
{
	int hexX;
	int hexY;
	int terrainType;
	struct mapHexTravel hexTravel[20];
};

// regular globals
int		mapWidth;				// used to store the width of the loaded map

// here is the function that loads it
void map::loadMap(char* mapname)
{
	int numberPaths;
	int mapHeight;
	int mapSize;
	int hexTerrainType;
	int hexX;
	int hexY;
	ifstream inFile(mapname);
	if (inFile.fail())
	{
		WriteError("Failed to open file to load in map data");
	}
	inFile >> mapHeight >> mapWidth >> ws;
	mapSize = mapHeight * mapWidth;
	writeError(mapHeight);
	writeError(mapWidth);
	WriteError(mapSize);
	//mapHex* tempMapHex;
	//tempMapHex = new mapHex[mapSize];
	mapHex tempMapHex[40];
	for(int x=0;x<mapSize;x++)
	{
		inFile >> hexX >> hexY >> hexTerrainType;
		inFile >> numberPaths;
		writeError(hexX);
		writeError(hexY);
		writeError(hexTerrainType);
		writeError(numberPaths);
		if(numberPaths == 0)
			inFile >> ws;
		else
		{
			for(int m=0;m<numberPaths;m++)
			{
				inFile >> tempMapHex[( hexY * mapWidth ) + x].hexTravel[m].destHexX >> tempMapHex[( hexY * mapWidth ) + x].hexTravel[m].destHexY
                                         >> tempMapHex[( hexY * mapWidth ) + x].hexTravel[m].movePoints;
				writeError(tempMapHex[( hexY * mapWidth ) + x].hexTravel[m].destHexX);
				writeError(tempMapHex[( hexY * mapWidth ) + x].hexTravel[m].destHexY);
				writeError(tempMapHex[( hexY * mapWidth ) + x].hexTravel[m].movePoints);
			}
			inFile >> ws;
			WriteError(" ");
		}
		tempMapHex[( hexY * mapWidth ) + x].hexX = hexX;
		tempMapHex[( hexY * mapWidth ) + x].hexY = hexY;
		tempMapHex[( hexY * mapWidth ) + x].terrainType = hexTerrainType;
	}
	inFile.close();
	mapArray = tempMapHex;
}
map mainMap;




here it the file reading in
5 4
0 0 1 2 1 0 4 0 1 4
1 0 1 4 0 0 4 2 0 4 0 1 4 1 1 4
2 0 2 4 1 0 4 3 0 4 1 1 4 2 1 4
3 0 1 3 2 0 4 2 1 4 3 1 4
0 1 1 5 0 0 4 1 0 4 1 1 4 0 2 4 1 2 4
1 1 1 6 1 0 4 2 0 4 0 1 4 2 1 4 1 2 4 2 2 4
2 1 2 6 2 0 4 3 0 4 1 1 4 2 2 4 3 2 4 3 1 4
3 1 1 3 3 0 4 2 1 4 3 2 4
0 2 1 3 0 1 4 1 2 4 0 3 4
1 2 1 6 0 1 4 1 1 4 2 2 4 1 3 4 0 3 4 0 2 4
2 2 2 6 1 1 4 2 1 4 3 2 4 2 3 4 1 3 4 1 2 4
3 2 1 5 2 1 4 3 1 4 3 3 4 2 3 4 2 2 4
0 3 1 5 0 2 4 1 2 4 1 3 4 1 4 4 0 4 4
1 3 1 6 1 2 4 2 2 4 2 3 4 2 4 4 1 4 4 0 3 4
2 3 2 6 2 2 4 3 2 4 3 3 4 3 4 4 2 4 4 1 3 4
3 3 1 3 3 2 4 3 4 4 2 3 4
0 4 1 2 0 3 4 1 4 4
1 4 1 4 0 4 4 0 3 4 1 3 4 2 4 4
2 4 2 4 1 4 4 1 3 4 2 3 4 3 4 4
3 4 1 3 2 4 4 2 3 4 3 3 4



and here is the logfile when i leave it to determine the size and sets it at 20
5 4 20
0 0 1 2 1 0 4 0 1 4  
1 0 1 4 0 0 4 2 0 4 0 1 4 1 1 4  
2 0 2 4 1 0 4 3 0 4 1 1 4 2 1 4  
3 0 1 3 2 0 4 2 1 4 3 1 4  
0 1 1 5 0 0 4 1 0 4 1 1 4 0 2 4 1 2 4  
1 1 1 6 1 0 4 2 0 4 0 1 4 2 1 4 1 2 4 2 2 4  
2 1 2 6 2 0 4 3 0 4 1 1 4 2 2 4 3 2 4 3 1 4  
3 1 1 3 3 0 4 2 1 4 3 2 4  
0 2 1 3 0 1 4 1 2 4 0 3 4  
1 2 1 6 0 1 4 1 1 4 2 2 4 1 3 4 0 3 4 0 2 4  
2 2 2 6 1 1 4 2 1 4 3 2 4 2 3 4 1 3 4 1 2 4  
3 2 1 5 2 1 4 3 1 4 3 3 4 2 3 4 2 2 4  
0 3 1 5 



[Edited by - TalonSnow on June 16, 2005 8:48:45 AM]

Share this post


Link to post
Share on other sites
Advertisement
Shouldn't all your ( hexY * mapWidth ) + x be ( hexY * mapWidth ) + hexX? Otherwise when x is 12, hexY is 3 and mapWidth is 4 you're accesing element 24 of a 20 element array.

Enigma

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!