Hello when I run this program for calculating the gcd through valgrind (this is the portion that is causing errors):
int gcd( int a, int b ) {
if( a == 0 || b == 0 )
return a + b;
if( a < b )
return gcd(b - a, a);
else
return gcd(a - b, b);
}
int main(int argc, char **argv) {
int a = atoi( argv[1] );
int b = atoi( argv[2] );
int q = gcd(a, b);
fprintf(stdout, "%d\n", q);
return 0;
}
With no arguments I get
==22833== Invalid read of size 1
==22833== at 0x3685636EB2: ____strtol_l_internal (in /lib64/libc-2.12.so)
and when I run it using two negative numbers ex: 'gcd -5 -4' I get
==516== Stack overflow in thread 1: can't grow stack to 0x7fe601ff8
I believe the second errors (with the negative number inputs) is because of the a < b part is this true? What part in the code is causing error 1 on its own?