Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.

graveyard filla

getting bugs trying to make dynamic arrays

This topic is 5392 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

high, i have a ascii based RPG that ive been working on. anyway, there is a LoadMap() function which reads in a map file (.txt) and makes a char array (the screen) out of it. originally had it so this LoadMap() function read in a txt file that was filled with #''s (for walls) and 0''s (for empty spots). when it found a # it would fill that spot of the array with a wall and when it found a 0 it would fill that spot in with a empty space. a map.txt would look something like this 21 7 ##################### 00000000000000000000# #0000##000###0000000# ##################### 00000000000000000000# #0000##000###0000000# ##################### etc. where the first int (21) would be the legth of the map and the second (7) would be the width. anyway, the monsters/player/treasure chest, ETC. that made up the rest of the game were drawn in in other functions like InitPlayer() or InitMonsters() etc. anyway, i wanted to make the game more flexible and easier to make multiple maps, so i decided to add the ability to put not only empty spaces and walls in the map.txt, but also be able to put the players starting point, the ending point, treasure chest, and the monsters. well, the starting point, ending point, and treasure chest were esay to do. i just modified the LoadMap() function to read in not only 0''s and #''s but also T''s and E''s etc. and fill in that spot with the chest or the end position or whatever. then i would go to fucntion that drew it and get rid of it there, because it is now drawn in the LoadMap function. but with monsters im having a problem. monsters is an array because there is more then 1 monster in a map. all the others were just a single seperate character, i only needed 1 in every map so i didnt need to make a dynamic array for them. for monsters i added a 3rd line to the map file after the legth and the width, i added a number_of_monsters variable. so it would read in however many monsters that were in that map and make a char array big enough to hold that many monsters. but heres the problem- everything works fine, as long as i make 6 monsters in one map or less. if i try putting 7 monsters or more in the map, the game doesnt work. the map wont load and the program crashes. i dont understand why. why would 6 monsters work perfectily fine but anything more then that crash? its really bothering me and ive been trying to fix it for so long but im getting very frustrated. im very new to doing dynamic memory stuff so i know i must be doing something wrong. im hoping one of you could point it out for me. thanks for any help. ps, heres the source for the LoadMap fucntion. thanks!!!
bool LoadMap()

	string filename[4]; //this holds the string of which map to load

	filename[0] = "Map1.txt";
	filename[1] = "Map2.txt";
	filename[2] = "Map3.txt";
	filename[3] = "Map4.txt";

	// Create a "ifstream" (way to read from a file) for the map text file

	ifstream file_in(filename[gamedata.current_level - 1].c_str());
	//loads the map for whatever level it is which was determined the line above this

	// Error Check -- If we couldn''t open the file, we can''t load the map

		return false;

	// First line of "map file" contains two integers.

	// First int is the width of the map

	// Second int is the height of the map

	file_in >> gamedata.width; // Read in the width of the map

	file_in >> gamedata.height; // Read in the height of the map

	file_in >> gamedata.number_of_monsters;  //read in how many "monsters" are on this map

//	char *monsters = new char[gamedata.number_of_monsters]; 

	// Now we can create a CHAR_INFO array that is big enough to hold the map

	gamedata.screenBuff = new CHAR_INFO[gamedata.width * gamedata.height];
	gamedata.monsters = new char[gamedata.number_of_monsters];
	//make a new set of monsters depending on how many are specified in the map

		// Error check -- Make sure we could allocate the memory

		if(gamedata.screenBuff == NULL || gamedata.monsters == NULL)
			file_in.close(); // Close the file we opened

				return false;

	//for (int i = 0 ; i < gamedata.number_of_monsters ; i++)

		//fill up each monster with color and an acii char


	//monster[i].dispChar = (char)234;

	//monster[i].color = gamedata.current_level_floor | FOREGROUND_GREEN | FOREGROUND_INTENSITY;

	//if (i > 0)

	//	monster[i].color = gamedata.current_level_floor | FOREGROUND_RED;

	//else if (i > 1)

	//	monster[i].color = gamedata.current_level_floor | FOREGROUND_BLUE;


	//i = 0;

		// These will be used to index into our screenBuff

	// *Remember* even though "screenBuff" is a one dimensional array, we''re going to

	// treat it as a two dimensional array

	int x = 0;
	int y = 0;
	int i = 0; // a counter used in the Monster part of the map placement


	char temp; // This will be a "temp" char -- This will hold the current character

			  // we''ve read in from the file

	while(y != gamedata.height) // Keep going until we reach the end of the "map"

		file_in >> temp;

			case ''0'': // We''ve read in an "open space" in our maze

				// Fill the current location in the screen buffer with a space character ('' '')

				gamedata.screenBuff[x + y * gamedata.width].Char.AsciiChar = '' '';
				gamedata.screenBuff[x + y * gamedata.width].Attributes = gamedata.current_level_floor; 

			case ''#'':  // We''ve read in a "wall" in our maze

				// Fill the current location in the screen buffer with the character 

				// defined by WALL 

				gamedata.screenBuff[x + y * gamedata.width].Char.AsciiChar = WALL;
				gamedata.screenBuff[x + y * gamedata.width].Attributes = gamedata.current_level_wall_BG | gamedata.current_level_wall_FG; 

			case ''E'':
				gamedata.screenBuff[x + y * gamedata.width].Char.AsciiChar = ''E'';
				gamedata.screenBuff[x + y * gamedata.width].Attributes = gamedata.current_level_wall_BG | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; 
				gamedata.xEndPos = x;
				gamedata.yEndPos = y;

			case ''T'':
				gamedata.screenBuff[x + y * gamedata.width].Char.AsciiChar = ''T'';
				gamedata.screenBuff[x + y * gamedata.width].Attributes = gamedata.current_level_wall_BG | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; 
				gamedata.xTreasPos = x;
				gamedata.yTreasPos = y;

			case ''M'':
				monster[i].dispChar = (char)234;
				monster[i].color = gamedata.current_level_floor | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
				monster[i].xPos = x; //give this monster a unique identity (monster[i] where i is unique)

				monster[i].yPos = y;
				gamedata.screenBuff[x + y * gamedata.width].Char.AsciiChar = monster[i].dispChar;
				gamedata.screenBuff[x + y * gamedata.width].Attributes = monster[i].color; //;gamedata.current_level_wall_BG | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | BACKGROUND_RED; 

				i++; //increment to the next monster


			default: // If we read any other character (char), there''s an error in our 

					// map file -- So close it and return

					return false;

		// Increment ''x'' every time we write a character until ''x'' equals the 

		// width of map, then start ''x'' back at zero

		if(++x == gamedata.width)
			x = 0;
			y++; // We need to increment y so we effectively "go down to the next 

				// line" in our screen buffer


	} // end of while(y != gamedata.height)

	// Okay if we get here the map has been loaded successfully --

   // Now all we have to do is close the file we opened and then return true

			return true; // Success :)

} // end of bool LoadMap()


Share this post

Link to post
Share on other sites
You need to do a big more debugging. Put a brakpoint in the monster case and see what happens for the 7th monster - has the array been allocated high enough? And what is the error message you get? Try also to use better variables like numMonsters and currentMonster and then put checks in like assert(currentMonster
Also you maybe better off using a vector as it can dynamically grow as you add monsters.

Share this post

Link to post
Share on other sites
1) You seem to have a struct that represents your monsters, but you''re allocating an array of single characters to store them. Unless it''s deliberate that
2) "gamedata.monsters" doesn''t match "monster". Where is your "monster" array defined/declared? Is it possible that *that* one isn''t big enough? I don''t see that anything is being done with "gamedata.monsters" after the allocation.

3) Don''t check for NULL after new. It can''t give you that as a result. If the allocation failed, it will throw std::bad_alloc - you need a try/catch block to handle the exception.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!