Jump to content

Debugging code - variables in invalid locations (help?)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
_J_

_J_

    Newbie

  • Members
  • Pip
  • 6 posts
So I'm currently writing some code to drive AI. The code I've written (in C) builds and I'm debugging and I note an array isn't updating. So I watch a few more variables and find that it seems to think two variables, r and k have 'invalid location's. Not sure if this changes anything but I'm working in AVR Studio 4 and using the debugger in that program (and the device for the simulation is an ATmega48).

Here's a snippet of my code which is causing issues:
void Find_Possible_Moves(int game_state[48]){
   	uint8_t r;
	uint8_t col;
	uint8_t k=0;

	for(col=0;col<8;col++){
	    for(r=0;r<6;r++){
	        if(game_state[r*8+col] == 2){
	            //2=> no player owns that position
	            possible_moves[k]=r*8+col;
		k++;
		break;
         }
     }
}
In the debugger is comes up that the col values updates fine (and is stored in R21) but for r and k, the value is given as 'location not valid'. I'm just confused because initially I even had col and r declared on the same line and still col worked fine but r was given an invalid location.

I'm just not really sure what's going on here. Any insight anyone? The function I posted is called by a different part of the code. Thanks :)

Edited by WingedPanther, 20 May 2009 - 07:42 AM.
add code tags (the # button)


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Without seeing the possible_moves array, it's kind of hard to help out.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
_J_

_J_

    Newbie

  • Members
  • Pip
  • 6 posts
Ah sorry about that. Possible moves is an array of 8 uints: uint8_t possiblemoves[8];

Also, for some reason that part of the the code was fixed by adding in:

void Find_Possible_Moves(uint8_t game_state[tokens]){

	uint8_t row;

	uint8_t col;

	int k=0;


	for(col=0;col<8;col++){

	     for(row=0;row<6;row++){

		    if(game_state[row*8+col] == 2){

			    //2=> no player owns that combo

				possible_moves[k]=row*8+col;

				k++;

				break;

			}

			if(k==8){ //extra code for checking if possible moves is full

			    row=6;

			    col=8;

			}

         }

     }

} 

By changing k to an int (takes up more memory but if it works, I'm willing to deal with it; looks like we need a microcontroller with more memory at any rate) and adding those lines of code, the variables work now...
But another part of the code is having the same issue. I really need to figure out what is making these variables have an 'invalid location'. I guess I am mainly wondering if people have had issues with variables being labelled as having an 'invalid location' before. I'm not sure even if this is a debugger error because it is weird the way it randomly happens/doesn't happen.
Now to fix up my base cases so my recursion doesn't go into an infinite loop...

Edited by _J_, 21 May 2009 - 01:08 AM.
code tags...


#4
bvaessen

bvaessen

    Newbie

  • Members
  • PipPip
  • 27 posts
Mind that sometimes compiler optimizations cause weird problems like this. Usually you can switch them off in the settings. Sometimes it also helps declaring the variable as volatile.


---------------
Launch-IT :: Home

#5
_J_

_J_

    Newbie

  • Members
  • Pip
  • 6 posts
Wow, thank-you so much! I knew it would be something small but I forgot C preferred it if changing variables were declared as volatile. Declaring any of the variables given 'invalid location's as volatile fixed all of my problems! Thank-you again! (having variables which actually update makes debugging much easier :D).