Thread: Some kind of memory mistake. Please review

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

    Some kind of memory mistake. Please review

    The following code runs fine for denom <= 11, but it gives a segmentation fault with the gcc compiler and doesn't run at all with other compilers for denom > 11. Please help! Thanks.

    Code:
    #include <stdio.h>
    
    
    void arrage(long *a, long *b);
    void reduce(long *numer, long *denom, long gcd);
    int getGCD(long a, long b);
    int getLCD(long a, long b);
    
    main()
    {
      long denom, lcd, sumNumer = 1, sumDenom = 1;
    
      for(denom = 2; denom <= 11; denom++) {
        lcd = getLCD(sumDenom, denom);
        sumNumer = sumNumer * lcd / sumDenom + lcd / denom;
        sumDenom = lcd;
        reduce(&sumNumer, &sumDenom, getGCD(sumDenom, sumNumer));
      }
      printf("Sum is: %d/%d or %.5f in decimal form.\n", 
    	 sumNumer, sumDenom, (double)sumNumer / sumDenom);
      getchar();
    }
    
    void arrage(long *a, long *b)
    {
      int temp;
      if(*b > *a) {
        temp = *a;
        *a = *b;
        *b = temp;
      }    
    }
    
    void reduce(long *numer, long *denom, long gcd)
    {
      *numer = *numer / gcd;
      *denom = *denom / gcd;
    }
    
    int getGCD(long a, long b)
    {
      int r;
      arrage(&a, &b);
      r = a % b;
      if(r == 0)
        return b;
      else
        return getGCD(b, r);
    }
    
    int getLCD(long a, long b)
    {
      return a * b / getGCD(a, b);
    }
    Last edited by mfantastique; 11-11-2004 at 09:48 PM.

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    I think your recursive function is filling up the stack. It's not a question of < > 11, it's a question of hitting a number that causes your function to run so many times that if fills up the stack.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Azuth
    I think your recursive function is filling up the stack. It's not a question of < > 11, it's a question of hitting a number that causes your function to run so many times that if fills up the stack.
    Try this in getGCD, with denom >= 12:

    Code:
    int getGCD(long a, long b)
    {
      int r;
      arrage(&a, &b);
      printf("in getGCD: after arrag: a = %d, b = %d\n", a, b);
      r = a % b;
      printf("in getGCD: r = %d\n", r);
      if(r == 0)
        return b;
      else
        printf("Recursively Calling getGCD(%d, %d)\n", b, r);
        if ((b < 0) || (r < 0)) {
          getchar();
        }
        return getGCD(b, r);
    }
    (Ctrl-C gets you out of it.)

    Regards,

    Dave

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    When denom is 12, sumDenom is 27720 and sumNumer -68920. look at this stack frame:
    getGCD(long 27720, long -68920) line 47
    getGCD(long 27720, long -68920) line 51 + 13 bytes
    getGCD(long 27720, long -68920) line 51 + 13 bytes
    getGCD(long 27720, long -68920) line 51 + 13 bytes
    getGCD(long 27720, long -68920) line 51 + 13 bytes
    main() line 20 + 13 bytes
    See? stack overflow!

  5. #5
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Yup, and you don't wanna know what it looks like when denom hits 25 ! O_o
    Demonographic rhinology is not the only possible outcome, but why take the chance

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    4
    Thanks a lot everyone!

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    4
    I understood what you all said, so I tried coding the recursive method iteratively. I still exceed LONG_MAX, however, and % doesn't apply to floating data types. Please help! This is the latest version of the code:

    Code:
    #include <stdio.h>
    
    long getLCD(long num1, long num2);
    long getGCD(long num1, long num2);
    
    main()
    {
          int i;
          long numerator = 0, denominator = 1, lcd;
    
          for(i = 1; i <= 50; i++)
          {
                lcd = getLCD(denominator, i);
                numerator = numerator * lcd / denominator + lcd / i;
                denominator = lcd;
                printf("On iteration %d: NUM = %d, DEN = %d\n", 
                         i, numerator, denominator);
                printf("Sum of 1/n from 1 to 50 is %d/%d in fractional
                         form, %.3f in decimal form.\n", numerator,
                         denominator, (double) numerator / denominator);
          }
    
          getchar();
    }
    
    long getLCD(long num1, long num2)
    {
          return num1 * num2 / getGCD(num1, num2);
    }
    
    long getGCD(long num1, long num2)
    {
          int temp, rlast, rcurrent;
    
          if(num1 > num2)
          {
                temp = num1;
                num1 = num2;
                num2 = temp;
          }
    
          rlast = num2;
          rcurrent = num1 % rlast;
          while(rcurrent != 0)
          {
                temp = rlast;
                rlast = rcurrent;
                rcurrent = temp % rlast;
          }
    
          return rlast;
    }

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can use long long to get a 64-bit int.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Help with insert/delete binary search tree
    By Nazgulled in forum C Programming
    Replies: 39
    Last Post: 03-25-2009, 04:24 PM
  3. Copying memory, pointers and the like.
    By psychopath in forum C++ Programming
    Replies: 34
    Last Post: 12-12-2006, 01:37 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM