Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function and array 1

Status
Not open for further replies.

dignick

Technical User
Aug 17, 2003
43
GB
I have a program working quite well. I decided I wanted to declare one of the arrays I was using inside the only function it was being used in as it was not necessary to be global. It is declared as
Code:
float beta[1];
What I found quite surprised me. I'm not sure why it happens, but when I declare the exact same variable inside the function the program stops working properly (not sure exactly what goes wrong, the numbers it outputs are totally off). If I print the value of either [0] or [1] the value is the same as if the array is declared outside the function. If I set it to [2] while still in the function the program operates properly again.
It's not essential I solve this issue, but I'd like to understand why its happening.
 
I'm not sure without seeing some code, but one difference between global and local variables is that global variables are initialized to 0 while local variables just have garbage. Are you sure you aren't reading a value from your array that you haven't set yet?
 
Well there is a lot of code, I'm not sure which to include...plus its a university assignment, so don't want to give too much away for fear of plagiarism. I'll paste the function involved. I've tried to do as much testing as possible to narrow down what the problem is, but I still don't know. I hope you can reproduce the problem.
Also if you can see any way of improving the code please let me know.

Code:
float (all other variables);

void calc()
{
	float alpha,beta[2],a,b;
	i=0;
	x[i]=0.5;
	v[i]=0;
	
	alpha=0-c/(2*m);
	
	beta[0]=sqrt(0-(c*c-4*m*(s1)))/(2*m);

	beta[1]=sqrt(0-(c*c-4*m*(s1+s2)))/(2*m);


	while(i<(it/is))
	{
	 	t=0;
		
		a=(x[i]+(m*g/s1));
		b=((v[i]-alpha*a)/beta[0]);
		
		while(i<(it/is) && (0-x[i])<=d)
		{
			++i;
			t=t+is;
			x[i]=(exp(alpha*t)*((a*cos(beta[0]*t))+(b*sin(beta[0]*t))))-(m*g/s1);
			v[i]=(exp(alpha*t)*((beta[0]*((b*cos(beta[0]*t))-(a*sin(beta[0]*t))))+(alpha*((a*cos(beta[0]*t))+(b*sin(beta[0]*t))))));
		}
		
		t=0;
		
		a=(x[i]+(((m*g)+(s2*d))/(s1+s2)));
		b=((v[i]-(alpha*a))/beta[1]);
		
		while(i<(it/is) && (0-x[i])>d)
		{
			++i;
			t=t+is;
			x[i]=(exp(alpha*t)*((a*cos(beta[1]*t))+(b*sin(beta[1]*t))))-(((m*g)+(s2*d))/(s1+s2));
			v[i]=(exp(alpha*t)*((beta[1]*((b*cos(beta[1]*t))-(a*sin(beta[1]*t))))+(alpha*((a*cos(beta[1]*t))+(b*sin(beta[1]*t))))));
		}
	}
}
 
Wait a sec... You said you declared it as:
Code:
float beta[1];
But then you tried to access [0] & [1]. beta[1] is out of bounds since your array only has 1 float.
Maybe that was the bug?
 
I thought the size of the array included 0? It works like that if you declare it globally anyway. The strange thing is I can still read beta 0 and 1, they print the right values, but in calculation something goes horribly wrong...
 
Code:
int array[2];
will declare an array of 2 ints, which are referenced by array[0] & array[1]

You might be able to access array[2], but you don't own that piece of memory and it could get overwritten at any time.

Since your array was declared on the stack (i.e. not a pointer), as soon as something else was added to the stack, it would overwrite the memory at array[2]. Global variables are stored in a different section of memory, so that's probably why you didn't see the problem when it was global. But it's still a bug either way.
 
ahh ok. Thanks for helping me understand! I'm quite new to programming.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top