Array values "magically changing"

Hi All,

I have a question about values in arrays.
My array values seem to be “magically” changing as they move through this function…

  
  
witchOrNot[0] = 0;  
strawPoked[0] = 0;  
drawStrawberry[0] = 0;  
  
//strawberry1:  
	if((ard.getDigital(3)) == 1){  
		cout << "Witch or not: " << witchOrNot[0] << endl;  
		  
		if(strawPoked[0] == 0){		//means if strawberry 1 has not been poked yet  
			strawPoked[0] = 1;  
			cout << "Straw1 poked: " << strawPoked[0] << endl;  
			//at this point, witchOrNot[0]'s value has changed from 0 to 1>>>>WHY?!??!  
			cout << "wicthOrNot[0] AfterPoked: " << witchOrNot[0] << endl;  
			  
			if(witchOrNot[0] == 1){	//means if strawberry1 IS a witch		  
				witchOrNot[0] = 2;  
				witchCounter = witchCounter + 1;  
				drawStrawberry[0] = 3;  
				isAWitchSounds();  
				cout << "drawStrawberry[0] inside of fcn 1: " << drawStrawberry[0] << endl;  
				cout << "witch count after Straw1 poked: " << witchCounter << endl;  
			}  
			else if(witchOrNot[0] == 0){	//means if strawberry1 is NOT a witch  
				witchOrNot[0] = 3;  
				drawStrawberry[0] = 2;  
				witchCounter = witchCounter;  
				notAWitchSounds();  
				cout << "drawStrawberry[0] inside of fcn 2: " << drawStrawberry[0] << endl;  
				cout << "witch count after Straw1 poked: " << witchCounter << endl;  
			}  
		}  
	   //somehow strawPoked has magically changed from a value of 1 to a value of 3 here:  
		cout << "Has Strawberry 1 been poked? " << strawPoked[0] << endl;  
	}	  
  

My console Reads:

Witch or not: 0
Straw1 poked: 1
wicthOrNot[0] AfterPoked: 1
drawStrawberry[0] inside of fcn 1: 3
witch count after Straw1 poked: 1
Has Strawberry 1 been poked? 3
witchOrNot outside of fcn: 3/0/1
drawStrawberry outside of fcn: 3-0-1
witchCounter outside of fcn: 1

What I have noticed, as the console and comments suggest, is that the array values are changing through the function. Does this have something to do with pointers or some other C++ concept that I am totally missing here?

Thank you very much for any replies or help. Let me know if you need more info.

~VeeBee

Hi there!

How do you define your variables in that code?

Have you tried a debugger?

Cheers,

punkto

Hi Punkto,

Thanks for your reply.

I define these array variables in my code in testApp.h as:

  
int witchOrNot[];  
	int strawPoked[];  
	int drawStrawberry[];  
	int strawberry[];  

Then in setup, they are declared like this:

  
   for (int i = 0; i < numStrawberries; i++){   
		witchOrNot[i] = 0;  
		strawPoked[i] = 0;  
		drawStrawberry[i] = 0;  
		cout << "setup: " << "draw Strawberry " << i << ": " << drawStrawberry[i] << endl;  
	}  

I am not very familiar with using a debugger. How would a debugger be useful if the code is successfully compiling?

Thanks Again.

VeeBee,

it looks to me as if you’ve made arrays with no size - i.e. zero elements. Since C/C++ is pretty loose, it’s allowing you to just trump around in whatever general memory comes right after your blank arrays. Your results will be undefined, and you’re probably overwriting other values you care about too.

You have a few choices:
Define the arrays at a fixed size at compile time (int strawPoked[10]:wink: - simplest approach

Make an array of int pointers, and use ‘new’ to create them when you know the size, then use pointer syntax when accessing your values - this may exceed your current programming knowledge, and has the usual C pointer problems, such as potential for memory leaks and invalid pointer dereferencing

Use a more modern container than a C array, which will let you work about the way you expect. I’m new to oF, maybe someone can chime in with the recommended container type?

Bruce

Hi,

I agree with bwheaton.
Try vector types which are dynamic arrays. Just create an array, and add an element when you need to to expand it.