-1

This code was working but some compiler throws a segmentation fault. If I comment out sumNumber then the error does not happen.

int sumNumber(int array[], int k);


int main(void) {

        int n;
        static int num[MAX];

        int k;

        scanf("%d", &n);
        scanf("%d", &k);


        for(int i = 0; i < n; i++){
            scanf("%d", &num[i]);
        }

        quick_sort(num,0,n-1);
        printf("%d\n",sumNumber(num,k-1));

      return 0;
    }

int sumNumber(int array[],int k){


    if(k < 0){
        return 0;
    }
    return (array[k] + sumNumber(array,k-1));
}
CodeMouse92
  • 6,840
  • 14
  • 73
  • 130
윤이래
  • 3
  • 1
  • 2
    Sounds like a [stack overflow](http://stackoverflow.com/questions/33154028/segmentation-fault-in-c-and-recursion-function) to me. – David Schwartz Oct 15 '15 at 16:43
  • Maybe there's a bug in `quick_sort`. Misusing pointers can leave time bombs that don't go off until some other code runs. – Barmar Oct 15 '15 at 16:45
  • 3
    Why do you need to sort the array? Addition is associative and commutative, so the order doesn't matter. – Barmar Oct 15 '15 at 16:46
  • Without `quick_sort()` which I don't have, this works perfectly well, but I wonder why you are passing to `quicksort()` arguments `0` and `n-1`. If the array has 1 element, is `quick_sort()` well-behaved when told the element size is `0` or there are no elements? – Weather Vane Oct 15 '15 at 16:56
  • you have to ensure that n and k are kept between 1 and MAX – milevyo Oct 15 '15 at 17:05
  • 1
    Possible duplicate of [Definitive List of Common Reasons for Segmentation Faults](http://stackoverflow.com/questions/33047452/definitive-list-of-common-reasons-for-segmentation-faults) – CodeMouse92 Oct 15 '15 at 17:31

1 Answers1

0

I can help.

I'm looking at:

printf("%d\n",sumNumber(num,k-1));

and I'm looking at:

int sumNumber(int array[],int k){


if(k < 0){
    return 0;
}
return (array[k] + sumNumber(array,k-1));
}

and based on these two code fragments alone, your program won't always work. If the value for k in printf is higher than roughly the stack space required to process the function multiplied by the value you chose, then you'll run into a stack overflow issue which can cause a segmentation fault since you're then trying to write into an invalid memory address.

What you want to do to avoid this is to either limit the number by changing:

int k;

scanf("%d", &n);
scanf("%d", &k);

to:

int k=0;

scanf("%d", &n);
while (k==0 || k > 100){
    scanf("%d", &k);
    if (k==0 || k > 100){
        printf("Number invalid\n");
    }
}

That way you'll force a user to enter a number from 1 to a certain amount which in this case is 100.

Another option is to modify the sumNumber function so that it doesn't become recursive. That way, you can allow higher numbers without problems.

Mike -- No longer here
  • 2,064
  • 1
  • 15
  • 37
  • thanks so i try it but it happened again int n; int num[MAX]; int k = 0; while(n == 0 || n > MAX){ scanf("%d", &n); } while (k==0 || k > n){ scanf("%d", &k); } – 윤이래 Oct 16 '15 at 02:44
  • you should set MAX to at least the same value as whatever n is (the first number the user enters when the program is running) – Mike -- No longer here Oct 16 '15 at 03:15
  • i see i solved this problem im trying your advice but it happend again so in this time i put integer at n and k when i put more than 20000 it happend but when i put less than 10000 my program running i think the program size is more bigger that i though anyway thank you for your help – 윤이래 Oct 16 '15 at 10:24
  • The way to get 20000 to work is to increase the stack size available to your program. In Windows you need to change this in your compiler settings. in Linux, use `ulimit -s ###` where `###` is the total stack space in KB the user can allocate across all running programs. – Mike -- No longer here Oct 16 '15 at 16:48