arrays - Why does this C program lead to a segmentation fault (core dumped)? -


i've tried solving math problem (https://projecteuler.net/problem=2) in c program leads segmentation fault. i've tried looking through code, searching on site using -wall , -wpedantic no avail. in code causing segmentation fault (core dumped)?

#include <stdio.h> #include <stdlib.h>  // calculates sum of fib numbers // below (non-inclusive) parameter num. int calculate(int num) {     int = 2, bytes_to_allocate;      // ---------- begin: memory allocation calculation ----------     // calculates exact number of fibs less num, , saves     // variable called "bytes_to_allocate".      int flist[3]; // small list of 3 ints calculate fib numbers.      flist[0] = 1;     flist[1] = 2;      // if statements in loop used move     // index proper place in order calculate     // every fib number less num.     while(1) {         if(i == 0) {             if(flist[i+1] + flist[i+2] >= num) {                 break;             }             flist[i] = flist[i+1] + flist[i+2];             = 1;         }         else if(i == 1) {             if(flist[i-1] + flist[i+1] >= num) {                 break;             }             flist[i] = flist[i-1] + flist[i+1];             = 2;         }         else if(i == 2) {             if(flist[i-1] + flist[i-2] >= num) {                 break;             }             flist[i] = flist[i-1] + flist[i-2];             = 0;         }         bytes_to_allocate++;     }      // ---------- end: memory allocation calculation ----------      // allocates right amount of bytes corresponding     // number of fibs below value num.     int* list = calloc(bytes_to_allocate, sizeof(int));      if(list == null) {         printf("malloc failed.\n");         exit(1);     }      list[0] = 1;     list[1] = 2;      // loop initializes fibs < num in list.     for(i = 2; < num; i++) {         if(list[i-1] + list[i-2] < num) {             list[i] = list[i-1] + list[i-2];         }         else { // if not less num             break;         }     }      // add of fibs in list (and cleared adresses)     int sum = 0;     for(i = 0; < num; i++) {         if(list[i] % 2 == 0) {             sum += list[i];         }     }      free(list); // frees allocated memory.      return sum; }  int main(void) {     int sum;     int num = 4000000;     sum = calculate(num);     printf("\nsum of even-valued fibs < %d: %d\n\n", num, sum);     return 0; } 

you're not allocating enough memory list. make big enough hold num numbers:

int* list = calloc(num, sizeof(int)); 

for issues this, valgrind friend. when ran code through it, said initialization loop writing past end of allocated memory.

edit:

doing saves time , code of counting number of fibs beforehand, in calculate before allocation can go away.

edit 2:

a simpler way doesn't require large memory footprint:

int calculate(int num) {     int prev1, prev2, curr;     int sum;      sum = 0;     prev1 = 0;     prev2 = 1;     curr = 1;      while (curr < num) {         if (curr % 2 == 0) {             sum += curr;         }         prev1 = prev2;         prev2 = curr;         curr = prev1 + prev2;     }     return sum; } 

Comments