Search code examples
cmathsegmentation-faultcoredump

segmentation fault (core dumped) error in a c program for combination function


#include <stdio.h>
#include <stdlib.h>

int factorial(int i) {
    if(i == 1) {
        return 1;
    }
    else {
        return i*factorial(i - 1);
    }
}

int combination(int l, int m) {
    return factorial(l)/(factorial(l-m)*factorial(m));
}

int main() {
    int n,r;
    printf("Input taken in form of nCr\n");
    printf("Enter n: ");
    scanf("%d", &n);
    printf("Enter r: ");
    scanf("%d", &r);
    int y = combination(n, r);
    printf("Result: %d", y);

    return 0;
}

Tried to make a simple code for calculating the combination function in maths. It worked for small values and basically works till n = 12, and gives wrong values from n = 13 and onwards. Also for n = 15 and r = 2, it returns the result -4. And it gives the error

segmentation fault (core dumped)

for n = 40 and r = 20. I would like to know how to solve this problem and why exactly is this happening.


Solution

  • This is an overflow problem here. You result is above the max int value.

    13! = 6227020800

    Wich is more than INT_MAX (2147483647). If you want to handle larger numbers you should either use other variables types (for example unsigned long long), or handle the overflow in your function to avoid memory crashes.

    Here is a topic that could be interesting about overflow checking in c here.

    Also for n = 15 and r = 2, it returns the result -4

    When a variable overflowed, it can underflow and overflow in cycle. This is why you are getting negative values. I'm not sure but I think this is related. If somebody can validate this it would be great.