-3

through my quest in c prog in VS code, I was required to implement a few simple functions, which I wanted to test. Below the code is attached, along with the error message I've been receiving. is there something I defined wrong in the code? or is the cause of this fault something else? thank you in advance!

#include <stdio.h>


float AbsVal(float a)
{
    float result;
    if (a-0==a)
        result=a;
    if (0-a==a)
        result=(-a);
    return result;

}


int gcd (int a, int b)
{
    int result;
    if (a==0 && b==0)
        result=0;
    if (a==0)
        result= b;
    if (b==0)
        result = a;
    if (a%2==0 && b%2==0)
        result= 2*gcd(a/2,b/2);
    if (a%2==0 && b%2!=0)
        result= 2*gcd(a/2,b);
    if (a%2!=0 && b%2==0)
        result= 2*gcd(a,b/2);
    if (a%2!=0 && b%2!=0)
        result= gcd(AbsVal(a-b),b);

    return result;

}

int main()
{
    AbsVal(3);
    AbsVal(-2);
    AbsVal(-3.1231);
    AbsVal(5.9876);
    gcd(15,3);
    gcd(60,73);
    gcd(456, 234);
    gcd(100,10);

    return 0;

}

fault message, while trying to run the .exe file:

Segmentation fault (core dumped)

note that I already looked at similar topics, but couldn't find the direct answer for my issue

rioV8
  • 24,506
  • 3
  • 32
  • 49
Sagilon
  • 1
  • 1
  • 1
    A core dump is excellent news - it means the program state was saved in a file which you can load in your debugger. If you don't know how, then this is the perfect time to learn. – Useless Oct 07 '21 at 09:47
  • Why do you implement your own "absolute" function instead of using the standard [`fabsf`](https://en.cppreference.com/w/c/numeric/math/fabs) function? And cosidering you use *integers* why not the standard [`abs`](https://en.cppreference.com/w/c/numeric/math/abs) function? – Some programmer dude Oct 07 '21 at 09:48
  • As for your problem, I'll bet that none of the `a == 0` or `b == 0` comparisons will ever be true, leading ot infinite recursion and a stack overflow. See [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Some programmer dude Oct 07 '21 at 09:49
  • 1
    `gcd()` function is recursive and `valgrind` tells me `Stack overflow in thread #1: can't grow stack to 0x1ffe801000` – MarcoLucidi Oct 07 '21 at 09:50
  • 1
    Because you do not have any pointers here the only source of the segfault it stack overflow when the recursive function calls itself. Your condition to return `result` is never met and function is calling itself indefinitely – 0___________ Oct 07 '21 at 09:50
  • @0___________ so what kind of solution do you suggest, to solve this? also for prev comments, I know that the abs function exists, but that was the purpose, to implement it by myself. – Sagilon Oct 07 '21 at 09:57
  • why not use `inline float AbsVal(float a) { return a < 0 ? -a : a; }` – rioV8 Oct 07 '21 at 10:06
  • Lean to use `else if`, or just `return` directly rather than setting `result` and then returning later. In any case, the gcd algorithm is utterly buggy and not even close to correct. It is hopelessly flawed. It only looks for factors of 2, and it doesn't do so correctly. Learn to use the [Euclidean Algorithm](https://en.wikipedia.org/wiki/Euclidean_algorithm) instead. – Tom Karzes Oct 07 '21 at 10:14

1 Answers1

1

Your logic is wrong and it leads to infinite recursion which sooner or later leads to stack overflow and likely a seg fault.

As an example let's see what happens when gcd is called with both a and b being zero.

int gcd (int a, int b)
{
    int result;
    if (a==0 && b==0)   // When a and b are both zero we do
        result=0;       // result = 0
    if (a==0)           // When a and b are both zero we do
        result= b;      // result = b
    if (b==0)           // When a and b are both zero we do
        result = a;     // we do result = a
    if (a%2==0 && b%2==0)        // When a and b are both zero we do
        result= 2*gcd(a/2,b/2);  // a call of gcd with both a and b being zero
                                 // so we have an infinite loop

Now for cases where a and b are not both zero, one of the following must be true:

if (a%2==0 && b%2==0)
    result= 2*gcd(a/2,b/2);       // recursive call
if (a%2==0 && b%2!=0)
    result= 2*gcd(a/2,b);         // recursive call
if (a%2!=0 && b%2==0)
    result= 2*gcd(a,b/2);         // recursive call
if (a%2!=0 && b%2!=0)
    result= gcd(AbsVal(a-b),b);   // recursive call

So again we have recursive calls for all cases.

I haven't looked into the logic of the function but maybe you are missing some else keywords. Like:

int gcd (int a, int b)
{
    int result;
    if (a==0 && b==0)
        result=0;    
    else if (a==0)   
        result= b;   
    else if (b==0)   
        result = a;  
    // and so on

Further it's unclear what you expect this line to do:

if (a-0==a)

did you intend to do

if (a >= 0)
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63