   getting bugs trying to make dynamic arrays
  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 this21 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(){Randomize_Map_Color();string filename[4]; //this holds the string of which map to loadfilename[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 fileifstream 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 mapif(!file_in)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 mapfile_in >> gamedata.width; // Read in the width of the mapfile_in >> gamedata.height; // Read in the height of the mapfile_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 mapgamedata.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 memoryif(gamedata.screenBuff == NULL || gamedata.monsters == NULL){file_in.close(); // Close the file we openedreturn false;}//for (int i = 0 ; i < gamedata.number_of_monsters ; i++)//fill up each monster with color and an acii char//{//monster.dispChar = (char)234;</font><font color=gray>//monster.color = gamedata.current_level_floor | FOREGROUND_GREEN | FOREGROUND_INTENSITY;</font><font color=gray>//if (i &gt; 0)</font><font color=gray>//monster.color = gamedata.current_level_floor | FOREGROUND_RED;</font><font color=gray>//else if (i &gt; 1)</font><font color=gray>//monster.color = gamedata.current_level_floor | FOREGROUND_BLUE;</font><font color=gray>//}</font><font color=gray>//i = 0;</font><font color=gray>// These will be used to index into our screenBuff</font><font color=gray>// *Remember* even though "screenBuff" is a one dimensional array, we''re going to</font><font color=gray>// treat it as a two dimensional array</font><font color=blue>int</font> x = 0;<font color=blue>int</font> y = 0;<font color=blue>int</font> i = 0; <font color=gray>// a counter used in the Monster part of the map placement</font><font color=blue>char</font> temp; <font color=gray>// This will be a "temp" char – This will hold the current character</font>  <font color=gray>// we''ve read in from the file</font><font color=blue>while</font>(y != gamedata.height) <font color=gray>// Keep going until we reach the end of the "map"</font>{file_in &gt;&gt; temp;<font color=blue>switch</font>(temp){<font color=blue>case</font> ''0'': <font color=gray>// We''ve read in an "open space" in our maze</font><font color=gray>// Fill the current location in the screen buffer with a space character ('' '')</font>gamedata.screenBuff[<font color=purple>x + y * gamedata.width</font>].Char.AsciiChar = '' '';gamedata.screenBuff[<font color=purple>x + y * gamedata.width</font>].Attributes = gamedata.current_level_floor; <font color=blue>break</font>;<font color=blue>case</font> ''#'':  <font color=gray>// We''ve read in a "wall" in our maze</font><font color=gray>// Fill the current location in the screen buffer with the character </font><font color=gray>// defined by WALL </font>gamedata.screenBuff[<font color=purple>x + y * gamedata.width</font>].Char.AsciiChar = WALL;gamedata.screenBuff[<font color=purple>x + y * gamedata.width</font>].Attributes = gamedata.current_level_wall_BG | gamedata.current_level_wall_FG; <font color=blue>break</font>;<font color=blue>case</font> ''E'':gamedata.screenBuff[<font color=purple>x + y * gamedata.width</font>].Char.AsciiChar = ''E'';gamedata.screenBuff[<font color=purple>x + y * gamedata.width</font>].Attributes = gamedata.current_level_wall_BG | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; gamedata.xEndPos = x;gamedata.yEndPos = y;<font color=blue>break</font>;<font color=blue>case</font> ''T'':gamedata.screenBuff[<font color=purple>x + y * gamedata.width</font>].Char.AsciiChar = ''T'';gamedata.screenBuff[<font color=purple>x + y * gamedata.width</font>].Attributes = gamedata.current_level_wall_BG | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; gamedata.xTreasPos = x;gamedata.yTreasPos = y;<font color=blue>break</font>;<font color=blue>case</font> ''M'':monster[<font color=purple>i</font>].dispChar = (<font color=blue>char</font>)234;monster[<font color=purple>i</font>].color = gamedata.current_level_floor | FOREGROUND_GREEN | FOREGROUND_INTENSITY;monster[<font color=purple>i</font>].xPos = x; <font color=gray>//give this monster a unique identity (monster where i is unique)</font>monster[<font color=purple>i</font>].yPos = y;gamedata.screenBuff[<font color=purple>x + y * gamedata.width</font>].Char.AsciiChar = monster[<font color=purple>i</font>].dispChar;gamedata.screenBuff[<font color=purple>x + y * gamedata.width</font>].Attributes = monster[<font color=purple>i</font>].color; <font color=gray>//;gamedata.current_level_wall_BG | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | BACKGROUND_RED; </font>i++; <font color=gray>//increment to the next monster</font><font color=blue>break</font>;<font color=gray>//gamedata.</font><font color=blue>default</font>: <font color=gray>// If we read any other character (char), there''s an error in our </font><font color=gray>// map file – So close it and return</font>file_in.close();<font color=blue>return</font> <font color=blue>false</font>;}<font color=gray>// Increment ''x'' every time we write a character until ''x'' equals the </font><font color=gray>// width of map, then start ''x'' back at zero</font><font color=blue>if</font>(++x == gamedata.width){x = 0;y++; <font color=gray>// We need to increment y so we effectively "go down to the next </font><font color=gray>// line" in our screen buffer</font>}} <font color=gray>// end of while(y != gamedata.height)</font><font color=gray>// Okay if we get here the map has been loaded successfully –</font>   <font color=gray>// Now all we have to do is close the file we opened and then return true</font>   file_in.close();<font color=blue>return</font> <font color=blue>true</font>; <font color=gray>// Success :)</font>} <font color=gray>// end of bool LoadMap()</font></pre><!–ENDSCRIPT–>anyway,    
  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(currentMonsterAlso you maybe better off using a vector as it can dynamically grow as you add monsters. ;  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 that2) "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. 