   Wierd Array Errors
  Hi guys. I'm working on a little rogue-like game, and have run into a wierd error. This compiles fine, but when save_map() is called, an error is found and the application exits with an error message. Running through the program with a debugger, this error message is displayed: Unhandled exception at 0x010f1ad0 in Supa Basic.exe: 0xC0000005: Access violation reading location 0x010f8000.The 0x010f80000 location is my map[20][40] array, but I'm not sure how to fix this error.void save_map() {message("Saving...");int x=0;int y=0;do {do {switch (map[y][x]){message("4");//floorcase 0: save<<"."; break;//wallcase 1: save<<"#"; break;//down stairscase 2:save<<">";break;//goblincase 3:save<<"g";}x++;} while (x!=40); save<<"\n";y++;} while (y!=20);message("Game Saved!");}int map[20][40]= {0,1,0,0,3,0,0,0,0,0,0,0,...};
  When you loop the second time when y=1, x doesn't get set to zero; so you go from [0][0] to [0][39], then the next row from [1][40] to [1][79].  Also, use for-loops instead of do-whiles; the intention is clearer with for-loops than do-whiles:for ( int y = 0; y < 20; ++y ) {for ( int x = 0; x < 40; ++x ) {   /* ... */    }}*Epic bug fix for great justice![Edited by - _fastcall on May 7, 2010 4:19:16 PM];  Ah, thanks. No wonder the problem was with array values. And there's no subscript checking in C++!;Quote:Original post by darkcrobatAnd there's no subscript checking in C++!If you want subscript checking in C++, you'd use std::vector or boost::multi_array instead of raw arrays.Also,Quote:Unhandled exception at 0x010f1ad0 in Supa Basic.exe: 0xC0000005: Access violation reading location 0x010f8000.If you're using your degbugger correctly, you can see which line of code is causing this error (which should be at the switch( map[y][x] )).  Given the description of the error, you can deduce that either y or x is an invalid value.  Then you can look at the auto/local/watch/immediate mode window to inspect the values of y and x.  In which case, y should be 1 and x should be 40 at this point.  Then from here, you can make the appropriate fixes:  Set x to 0 after incrementing y => change the do-while loops to for-loops.   ;instead of ->   int map[20][40] and company,#define MAXWIDTH 20#define MAXHEIGHT 40int map[MAXWIDTH][MAXHEIGHT];save_map(){int y=0;while (y < MAXWIDTH){int x=0;while (x<MAXHEIGHT){// switch code...x++;}y++;}}