# Problem allocating enough memory[SOLVED]

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

## 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
{
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 on other sites
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 on other sites
thanks ignima that was certainly the problem!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634134
• Total Posts
3015751
×