Thread: Unhandled Exception , access violation reading locations.

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    Unhandled Exception , access violation reading locations.

    Would you please tell me the way to correct this kind of error ??
    I have thought many many days and I simply cannot solve this . . . .

    Code:
    void win(Map*initialMap , Map* currentMap , int x , int y){
    	int i;
    	int j;
    	for(i=0; i < currentMap->height ; i++){				
    	// all boxes are pushed into all targets
    		for(j=0; j < currentMap->width ; j++){
    	if (currentMap->map2D[y+j][x+i]=='@' && initialMap->map2D[y-j][x-i]=='!' )  <-- error
    		return ;
    		}
    	}       
    }
    
    int try(Map *initialMap, Map *currentMap, int x, int y, int max_no_of_move, Stack *s) {
    	int i,j,k;
    	int result=0;
    	char q='\0';
    	char e[4]={NORTH,SOUTH,EAST,WEST};
    	Map tempmap;
    	if(max_no_of_move ==0 ) return 0;   
    	 win(&initialMap,&currentMap,&x,&y) return 1; 
    	  // if not , try each possible directions
    	for(i=0;i<4;i++){
    			if ( move_keeper(&initialMap,&currentMap,&x,&y,e[i])) {
    				 clone_map(&tempmap,&currentMap);
    				result  = try  ( &currentMap, &tempmap , x, y , max_no_of_move -1 , &s );
    					if(result) { return result;
    						//A solution is found
    				 		push(&s,e);
    				    	}
    						else {
    							q=pop(&s);
    			for(j=0;j<currentMap->width ; j++) {
    			for(k=0;k<currentMap->height;k++){
    							tempmap.map2D[k][j]=currentMap->map2D[k][j];
    			}}
    						}		
    						return  result;
    			}
    			}
    		
    	
    }
    Last edited by sokoban_1; 04-03-2010 at 02:14 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    OP needs to read << !! Posting Code? Read this First !! >>

    and this thread should be moved to C++ forum
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vart
    and this thread should be moved to C++ forum
    I think this is C, not C++, otherwise there would have been a compile error due to the use of a keyword as a function name.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    Indeed it is C . . .

    And I am thinking of the base case ( pushing all boxes into all targets .. )

    I am wondering if this code is correct or not . . .

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    void win(Map*initialMap , Map* currentMap , int x , int y){
    	int i;
    	int j;
    	for(i=0; i < currentMap->height ; i++){				
    	// all boxes are pushed into all targets
    		for(j=0; j < currentMap->width ; j++){
    	if (currentMap->map2D[y+j][x+i]=='@' && initialMap->map2D[y-j][x-i]=='!' )  <-- error
    		return ;
    		}
    	}       
    }
    You need a simpler and more transparent one, imo.

    Code:
    int isWon(int currentMap, int offsetR, int offsetC, int maxRowGoal, int maxColGoal) {
      int r, c;
      int won = 1;  //assume game is won
    
      for(r = 0; r < maxRowGoal; r++) {
        for(c = 0; c < maxColGoal; c++) {
          if(currentMap[r+offsetR][c+offsetC] != answerMap[r][c]) {
            won = 0;
            break;
          }
        }
      }
      return won;
    }
    The offsetR/C gives the starting row and column, and maxRow/ColGoal, gives the ending coordinates. imo a function to check if the game is won, should return SOMETHING to signify if the game is won or not.

    I have this game (at least the first 50 levels of one version of it by "Thinking Rabbit"), and there are some levels of it that the solution just makes me laugh.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-17-2008, 08:45 AM
  2. Access Violation using memcmp?
    By someprogr in forum C Programming
    Replies: 3
    Last Post: 12-31-2007, 01:45 AM
  3. Access violation... can't figure it out...
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2007, 10:52 AM
  4. Unhandled exception error in VC++
    By tandirovic in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2005, 05:16 PM
  5. 0xC0000005: Access Violation
    By Strider in forum Windows Programming
    Replies: 3
    Last Post: 11-07-2001, 02:46 PM