# Thread: Some kind of memory mistake. Please review

1. ## 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);
}```

2. 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.

3. 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. 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. Yup, and you don't wanna know what it looks like when denom hits 25 ! O_o

6. Thanks a lot everyone!

7. 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. You can use long long to get a 64-bit int.

Popular pages Recent additions