Double Array Member Variables

So I'm bored in my beginning C++ class having had plenty of amateur experience in similar languages, so I thought I'd make a little game out of the console window. What started out as a small self challenge to conquer boredom became a desperate quest to make this possible. My proffesor has been amazing and helped me as much as he could, but he only knows the basics by heart. Thus I have reached a predicament:

I am currently trying to load a 2D map of characters onto the screen that will represent a D&D-esque traverse-able grid. I have spent a lot of time optimizing this, and I feel like I am on the verge of getting it pretty well set up. I have a save file with the map data, and the program loads the dimensions of the map first, and then based on a basic loop and a 2D dynamic array, it stores it for printing, before promptly ridding itself of the data to free up the RAM. This approach worked fine, but when I went to move my code into the class as member functions things went to hell...here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <exception>
#include "Map.h"
using namespace std;

int main()
{
	Map LevelMap(1, 2, GetStdHandle(STD_OUTPUT_HANDLE));
	LevelMap.createMDM();
	LevelMap.loadMDM("Level1.txt");
      //LevelMap.draw(); 
	
	system("PAUSE");
	return 0;
}


LevelMap.draw is commented because the error occurs before that line
Map LevelMap(1, 2, GetStdHandle(STD_OUTPUT_HANDLE)) merely initializes the Map at (1,2) on the console window.
...here is the code for the functions above

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
//Creates space for the 2D MDM
int Map::createMDM(void)
{
	int result;
	if ((totalRows <= 0)||(totalCollums <= 0))
	{
		result = 0;
	}
	else
	{
	  //Creates data[row][collum]
		char ** data = new char * [(totalRows - 1)];
		for (int i = 0; i < totalCollums; i++)
		{
			data[i] = new char [(totalCollums - 1)]; //c-strings require the extra null char
		}
		result = 1;
	}
	return result;
}

//Loads the MDM from a .map file
void Map::loadMDM(string filename)
{
	ifstream levelFile;
	levelFile.open(filename);
	levelFile >> totalRows >> totalCollums;
	char * p = new char [totalCollums];
	levelFile.getline(p, totalCollums); //skip first line
	delete p;
	for (int i = 0; i < totalRows; i++)
	{
		levelFile.getline(data[i], totalCollums);
	}
	levelFile.close();
}


with appropriate headers and all...here is a list of the member variables:

1
2
3
4
5
private:
	COORD position;				//Co-ordinates on screen of the top left corner of the map
	int totalRows;				//Height of map
	int totalCollums;			//Height of map
	char ** data;				//Pointer to Dynamic Memory for Data 


When I run the code, I get the error:

Unhandled exception at at 0x7650C41F in RPG.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x003FF438.


I am assuming that the data pointer is being retained between member functions, but the array of pointers that data points to are not retaining their values. Anybody know how to fix this?
Last edited on
1
2
3
4
5
char ** data = new char * [(totalRows - 1)];
for (int i = 0; i < totalCollums; i++)
{
	data[i] = new char [(totalCollums - 1)]; //c-strings require the extra null char
}

You are creating a local variable named data when you should be using the member variable with that name.

Why are you allocating one row less than totalRows? If this is how it should be you have to make sure that that you do not access data[i] when i == totalRows - 1.

On the next line you are using totalCollums when it should be totalRows.

You probably want to use +1 instead of -1 when you allocate the row.
Last edited on
Oh, duh....that's stupid...sometimes it just takes a second set of eyes...ok thanks!

And I allocated totalCollums - 1 because the total amount of rows differs from the last row -> my first row is 0 (as is my first collum) and my totalRows is an integer count starting at 1...probably something I should smoothen out though.


I have removed the char initialization thus de-localizing data, and fixed that loop counter.

Upon recompiling, it is still returning the error:

Unhandled exception at at 0x7650C41F in RPG.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0040FB38.


Last edited on
Topic archived. No new replies allowed.