# Thread: Program stuck in infinite loop-->PLEASE HELP

1. ## Program stuck in infinite loop-->PLEASE HELP

I am currently working on a program in my computer science class that does various computations with complex numbers. For some reason, when the program gets to where it needs the DivCmplx (posted below), the program seems to be stuck in an infinite loop. I added in some print lines to see where it gets stuck. On my program, it inputs two complex numbers and repeatedly prints out the phrases "I can get here--NO PROBLEM" and "I can see the light...OVER AND OVER AGAIN!!!" This problem is using recursion and I think it has something to do with not getting the function to its base case, making it run the same recursion over and over. If anyone can help, it would be greatly appreciate!!!

Code:
```Cmplx DivCmplx(Cmplx z1, Cmplx z2)
{
/* Write this function  */
Cmplx z;
z = (Cmplx)malloc(sizeof(cmplxCDT));
printf("I get here -- NO PROBLEM!\n");
if (!z)
FatalError("Memory allocation error.\n",1);
if(z2->x == 0 && z2->y == 0){
printf("Arithmetic exception!\n");
free(z);
return NULL;
}
else if(z2->y == 0){
z->x = ((z1->x)/(z2->x));
z->y = ((z1->y)/(z2->x));
return z;
}
else
{
printf("I can see the light...OVER AND OVER AGAIN!!!\n");
free(z);
return DivCmplx(MulCmplx(z1, ConjCmplx(z2)), MulCmplx(z2,
ConjCmplx(z2)));
}
}```
Thanks,
Jacob

2. Put a printf into the beginning of the function that shows what DivCmplx was called with (z1 and z2) - that will show you the recursion and mostly likely you will see why you aren't converging to an answer. If this is floating point, it may be that you never actually reach z2->y == 0 and you may need to test as smaller than a very small value instead.

3. Originally Posted by Jedijacob
Code:
```Cmplx DivCmplx(Cmplx z1, Cmplx z2)
{
/* Write this function  */
Cmplx z;
z = (Cmplx)malloc(sizeof(cmplxCDT));
printf("I get here -- NO PROBLEM!\n");
if (!z)
FatalError("Memory allocation error.\n",1);
if(z2->x == 0 && z2->y == 0){
printf("Arithmetic exception!\n");
free(z);
return NULL;
}
else if(z2->y == 0){
z->x = ((z1->x)/(z2->x));
z->y = ((z1->y)/(z2->x));
return z;
}
else
{
printf("I can see the light...OVER AND OVER AGAIN!!!\n");
free(z);
return DivCmplx(MulCmplx(z1, ConjCmplx(z2)), MulCmplx(z2,
ConjCmplx(z2)));
}
}```
Thanks,
Jacob
You shouldn't continue to do anything if malloc fails. You however, dereference z as if nothing bad had happened, and then later try to free it. (While the free call shouldn't kill your program, you still shouldn't try to do anything with a null pointers. Which is what you do if malloc fails.)

And while this isn't the source of your problem, it is a problem potentially.

Quzah.

4. > else if(z2->y == 0)
I'm guessing these are floats or doubles right?
NEVER compare floats for equality. Floats are approximations, so whilst your calculation may look like it should return mathematical 0, the chances of it returning computational 0 is small. What you end up with is an approximate 0 instead. However, approximate 0 doesn't compare equal to 0, so you go round the loop again.

Code:
```int approxEqual( double a, double b ) {
return fabs(a-b) < 0.00001;
}```
Then you would write
Code:
`else if( approxEqual(z2->y,0) )`
Some background reading
http://docs.sun.com/source/806-3568/ncg_goldberg.html

Oh, and what crazy dude told you to cast the return result of malloc?

5. Originally Posted by Salem
Oh, and what crazy dude told you to cast the return result of malloc?
I thought this was common practice, since it returns a void pointer.

6. > I thought this was common practice, since it returns a void pointer.
It seems that less common practice is to read the FAQ
http://faq.cprogramming.com/cgi-bin/...&id=1043284351

Popular pages Recent additions