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?