I am having a very hard figuring out how to complete this assignment using double pointers. In this assignment, I am expected to open a text file containing information for a level map. We are expected to load this map as a char double pointer, and return that double pointer. If it isn't obvious, this is the first time I've tried to use double pointers.
I have a feeling I'm greatly overthinking this, because this part is not supposed to be hard. Normally I would just use a 2D array, but unfortunately I must use double pointers.
Text file information: If the information is relevant, the first line of the text file contains the width and height of the map, the second row contains the player position, and the following rows contain the contents of the map, each unit of space represented by a single char. for example, a 3 x 3 map may be formatted:
(edit: I'm having a really hard time getting the formatting for this to work. Hopefully my description is good enough)
The goal is to load that text file's information into a double pointer, not including the first two rows.
Right now, this is the code I have, which results in a memory leak. I am not allowed to change the function title or arguments. I have no idea what I'm doing, so this is probably pretty ugly.
char** loadMap(const string& fileName, int& maxRow, int& maxCol, Player& player) //not allowed to change
{
ifstream level;
level.open(fileName);
level >> maxRow >> maxCol >> player.row >> player.col;
//declare and assign memory ---> where I'm having trouble
char** map = new char*[999999];
for(int i = 0; i < 999999; i++) //maps can have a maximum height and width of 999,999 each.
{
map[i] = new char[999999];
}
for(int r = 0; r < 999999; r++) //row
{
for(int c = 0; c < 999999; c++) //column
{
level >> map[r][c]; //assign the next char in the level to map position [r][c]
}
}
return map;
}
I have also tried using malloc() instead of doing "new char[]", which gave me the same results. This is my first time posting here, so sorry if my post is a little wonky. Thanks!
Use the maxRow
and maxCol
values read in from the file to allocate the array instead of the 999999
values. Also change the loop conditions to use either maxRow
and maxCol
respectively. The rest of the code looks ok but has not been tested.
As the comments above have pointed out if maxRow
and maxCol
are anywhere near the 999999
value you will not have enough memory.
char** loadLevel(const string& fileName, int& maxRow, int& maxCol, Player& player) //not allowed to change
{
ifstream level;
level.open(fileName);
level >> maxRow >> maxCol >> player.row >> player.col;
//declare and assign memory ---> where I'm having trouble
char** map = new char*[maxRow];
for(int i = 0; i < maxRow; i++) //maps can have a maximum height and width of 999,999 each.
{
map[i] = new char[maxCol];
}
for(int r = 0; r < maxRow; r++) //row
{
for(int c = 0; c < maxCol; c++) //column
{
level >> map[r][c]; //assign the next char in the level to map position [r][c]
}
}
return map;
}