Maze Program - What am I doing wrong?

This is a discussion on Maze Program - What am I doing wrong? within the C++ Programming forums, part of the General Programming Boards category; I'm doing a school assignment where I have to randomly generate a maze, solve the maze, and print out the ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    13

    Maze Program - What am I doing wrong?

    I'm doing a school assignment where I have to randomly generate a maze, solve the maze, and print out the steps I took to solve it. I think I'm pretty close to solving it but when I run my program it crashes (no error in windows...just a message saying the program has stopped working).

    When I debug the program I get the following error:

    Unhandled exception at 0x6754d63c (msvcr80d.dll) in project3new.exe: 0xC0000005: Access violation writing location 0xcccccccc.

    What am I doing wrong?

    Code:
    #include <iostream>
    #include <ctime>
    #include <string>
    
    using namespace std;
    
    
    class maze{
    
    private:
    
    char mymaze[22][22];
    int moves;
    string movehistory[200];
    int mylocationx;
    int mylocationy;
    
    public:
    
    maze(){
    	moves=0;
    	   srand((unsigned)time(0)); 
    	for (int i=0;i<22;i++){
    		for(int e=0;e<22;e++){
        int random_integer; 
        for(int index=0; index<1; index++){ 
            random_integer = (rand()%3)+1; 
        }		if (random_integer==1){
    				mymaze[i][e]='X';
    			}
    			else{
    				mymaze[i][e]='O';
    			}
    		}
    	}
    	for (int i=0;i<22;i++){
    		mymaze [0][i]='X';
    		mymaze [i][0]='X';
    		mymaze [i][21]='X';
    		mymaze [21][i]='X';
    	}
    	mylocationx=1;
    	mylocationy=1;
    }
    
    void printmaze(){
    
    	for (int i=0;i<22;i++){
    		cout<<endl;
    		for(int e=0;e<22;e++){
    			cout<<mymaze[i][e]<<' ';
    		}
    	}
    }
    
    
    void moveup(){
    	movehistory[moves]="Up";
    	mylocationy++;
    	moves++;
    }
    
    void movedown(){
    	movehistory[moves]="Down";
    	mylocationy--;
    	moves++;
    }
    
    void moveleft(){
    	movehistory[moves]="Left";
    	mylocationx--;
    	moves++;
    }
    
    void moveright(){
    	movehistory[moves]="Right";
    	mylocationx++;
    	moves++;
    }
    
    
    bool canmoveup(){
    	if(mylocationx==1 && mylocationy==1){		
    		return false;
    	}
    	if (movehistory[moves]=="Down"){
    		return false;
    	}
    	int newy=mylocationy +1;
    	if(mymaze[mylocationx][newy]=='O'){		
    		return true;
    	}
    	else{
    		return false;
    	}
    }
    
    bool canmovedown(){
    	if (movehistory[moves]=="Up"){
    		return false;
    	}
    	int newy=mylocationy-1;
    	if(mymaze[mylocationx][newy]=='O'){		
    		return true;
    	}
    	else{
    		return false;
    	}
    }
    
    bool canmoveleft(){
    	if(mylocationx==1 && mylocationy==1){		
    		return false;
    	}
    	if (movehistory[moves]=="Right"){
    		return false;
    	}
    	int newx=mylocationx-1;
    	if(mymaze[newx][mylocationy]=='O'){		
    		return true;
    	}
    	else{
    		return false;
    	}
    }
    
    bool canmoveright(){
    	if (movehistory[moves]=="Left"){
    		return false;
    	}
    	int newx=mylocationx+1;
    	if(mymaze[newx][mylocationy]=='O'){		
    		return true;
    	}
    	else{
    		return false;
    	}
    }
    
    void printsolution(){
    
    	for (int i=0;i<22;i++){
    		cout<<endl;
    		for(int e=0;e<22;e++){
    			cout<<mymaze[i][e]<<' ';
    		}
    	}
    }
    
    bool end(){
    
    	if (mylocationx==20 && mylocationy==20){
    		return true;
    	}
    	else{
    		return false;
    	}
    }
    
    int getmoves(){
    return moves;
    }
    
    string gethistory(int movenumber){
    	return movehistory[movenumber];
    }
    };
    
    
    int main(){
    
    	maze gameboard;
    	gameboard.printmaze();
    	bool ended=false;
    
    	if(!gameboard.end()){
    		while (!ended){
    			if (gameboard.canmoveright()){
    				gameboard.moveright();
    				continue;
    			}
    			if(gameboard.canmovedown()){
    				gameboard.movedown();
    				continue;
    			}
    			if (gameboard.canmoveleft()){
    				gameboard.moveleft ();
    				continue;
    			}
    			if (gameboard.canmoveup()){
    				gameboard.moveup ();
    				continue;
    			}
    
    			if(!gameboard.canmoveright() && !gameboard.canmovedown() && !gameboard.canmoveleft() && !gameboard.canmoveup()){
    				cout<<"Escape is Impossible!"<<endl;
    				ended=true;
    			}
    		}
    	}
    	cout<<endl<<endl;
    	for(int i=0;i<gameboard.getmoves();i++){
    		cout<<gameboard.gethistory(i)<<endl;
    	}
    	cout<<endl;
    	gameboard.printsolution();
    	cout<<endl;
    }

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    It has to do with memory access, because you are dealing with arrays. Arrays can burn to the touch. Look carefully at your code to see how you might have assigned beyond the range of an array, deleted twice, etc.

    If you can't find it, or if you want safer code, replace all of your arrays with vectors, and replace all of your []'s with .at()'s.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    13
    The teacher asked for arrays so I'm afraid I'm stuck there. Thanks for the tip though. Now that I know what it is I have a better chance of finding where it is.

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Can you have your debugger point to the code where the crash is, and then unwind the stack to see where in your code it goes wrong?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    13
    I actually don't think I how to do that....when I run it in debug mode I get the unhandled exception that I listed before and the program breaks on line 324 of memcpy.asm (no idea what that file is).

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    13
    When I debug and I look at the call stack I get the below info..is that what you were talking about?

    Code:
    >	msvcr80d.dll!memcpy(unsigned char * dst=0xcccccccc, unsigned char * src=0x00418824, unsigned long count=4)  Line 324	Asm
     	msvcr80d.dll!memcpy_s(void * dst=0xcccccccc, unsigned int sizeInBytes=1245080, const void * src=0x00418824, unsigned int count=4)  Line 67 + 0x11 bytes	C
     	msvcp80d.dll!std::char_traits<char>::_Copy_s(char * _First1=0xcccccccc, unsigned int _Size_in_bytes=1245080, const char * _First2=0x00418824, unsigned int _Count=4)  Line 575 + 0x16 bytes	C++
     	msvcp80d.dll!std::_Traits_helper::copy_s<std::char_traits<char> >(char * _First1=0xcccccccc, unsigned int _Size=1245080, const char * _First2=0x00418824, unsigned int _Count=4, std::_Secure_char_traits_tag __formal={...})  Line 707 + 0x15 bytes	C++
     	msvcp80d.dll!std::_Traits_helper::copy_s<std::char_traits<char> >(char * _First1=0xcccccccc, unsigned int _Size=1245080, const char * _First2=0x00418824, unsigned int _Count=4)  Line 699 + 0x21 bytes	C++
     	msvcp80d.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign(const char * _Ptr=0x00418824, unsigned int _Num=4)  Line 1072 + 0x1d bytes	C++
     	msvcp80d.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign(const char * _Ptr=0x00418824)  Line 1081	C++
     	msvcp80d.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::operator=(const char * _Ptr=0x00418824)  Line 931	C++
     	project3new.exe!maze::moveleft()  Line 76 + 0x23 bytes	C++
     	project3new.exe!main()  Line 194	C++
     	project3new.exe!__tmainCRTStartup()  Line 597 + 0x19 bytes	C
     	project3new.exe!mainCRTStartup()  Line 414	C

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    13
    After looking through that I'm guessing there is something wrong with my moveleft function...I'll start there to find where the issue is

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Is it having trouble getting the string literal into movehistory[0]? I don't see why.

    The count in the function that fails is 4, so I think that's "Left".
    Last edited by CodeMonkey; 08-01-2007 at 08:43 PM. Reason: caps
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    If people would compile their programs in debug mode, then people would get a useful stack trace when their programs crash.

    This is like looking for a needle in a haystack...

    EDIT: Okay, a little late for that comment :-P

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    13
    I ran it a few more times and it seems like moveleft only fails sometimes. Other times it fails on moveright. This makes sense since the program looks at the maze to decide what to do.....but I still don't see what could be causing the failure. This is the stack when it looks like moveright failed

    Code:
    >	msvcr80d.dll!memcpy(unsigned char * dst=0xcccccccc, unsigned char * src=0x00418884, unsigned long count=5)  Line 324	Asm
     	msvcr80d.dll!memcpy_s(void * dst=0xcccccccc, unsigned int sizeInBytes=1245080, const void * src=0x00418884, unsigned int count=5)  Line 67 + 0x11 bytes	C
     	msvcp80d.dll!std::char_traits<char>::_Copy_s(char * _First1=0xcccccccc, unsigned int _Size_in_bytes=1245080, const char * _First2=0x00418884, unsigned int _Count=5)  Line 575 + 0x16 bytes	C++
     	msvcp80d.dll!std::_Traits_helper::copy_s<std::char_traits<char> >(char * _First1=0xcccccccc, unsigned int _Size=1245080, const char * _First2=0x00418884, unsigned int _Count=5, std::_Secure_char_traits_tag __formal={...})  Line 707 + 0x15 bytes	C++
     	msvcp80d.dll!std::_Traits_helper::copy_s<std::char_traits<char> >(char * _First1=0xcccccccc, unsigned int _Size=1245080, const char * _First2=0x00418884, unsigned int _Count=5)  Line 699 + 0x21 bytes	C++
     	msvcp80d.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign(const char * _Ptr=0x00418884, unsigned int _Num=5)  Line 1072 + 0x1d bytes	C++
     	msvcp80d.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign(const char * _Ptr=0x00418884)  Line 1081	C++
     	msvcp80d.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::operator=(const char * _Ptr=0x00418884)  Line 931	C++
     	project3new.exe!maze::moveright()  Line 83 + 0x23 bytes	C++
     	project3new.exe!main()  Line 187	C++
     	project3new.exe!__tmainCRTStartup()  Line 597 + 0x19 bytes	C
     	project3new.exe!mainCRTStartup()  Line 414	C
     	kernel32.dll!77423833() 	
     	[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]	
     	ntdll.dll!779cdbde()

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    13
    Ok I commented out any reference to the movehistory array and the program just hangs now instead of crashing. I think what's happening is that the maze isn't ending so the movehistory array was filling up and crashing me. Now that I know that I can look to see where the real issue is.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    void moveright(){
    	movehistory[moves]="Right";
    	mylocationx++;
    	moves++;
    }
    What happens when moves reaches 200, or higher? You access beyond the end of movehistory[], as it has 200 elements.

    [edit] Never mind, your latest post beat me to it. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    So you're going to have to make it grow, or let it be bigger from the start.

    Or.... you can strangle your teacher and use std::vector.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  14. #14
    Registered User
    Join Date
    Jul 2007
    Posts
    13
    That doesn't even seem to be the issue anymore...the program is hanging indefinately now. For some reason it isn't working it's way through the maze like I expected it to....I can't seem to find why yet though.

    I ran it once and it generated a maze where the only possible moves would be to move down once and be stuck at a dead end....but the stack shows that the moveleft() function caused the error. moveleft() shouldn't have come into play at all on that maze so now I'm trying to figure out what's going on.

  15. #15
    Registered User
    Join Date
    Jul 2007
    Posts
    13
    I think so far we can call this terminal programmer error on my part....I just realized that I've had my coordinates mixed up all along. (y for x and x for y) I'm not sure if that's what's causing this issue or not but it's something to fix to see what happens.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 01:03 PM
  5. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21