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

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    20

    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. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    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. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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.
    Hope is the first step on the road to disappointment.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Quote 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. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 07-06-2009, 07:12 AM
  2. Infinite loop!!!
    By Bizmark in forum C Programming
    Replies: 3
    Last Post: 02-21-2009, 12:09 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Program gets stuck in infinite loop
    By Xanth in forum C++ Programming
    Replies: 10
    Last Post: 02-08-2005, 12:51 AM