what was i trying to do is a recursion of first 40 fibonacci numbers, when tried to launch a program, it's stopped at return 0;.
#include <stdio.h>
#define SIZE 40
void sum(int arr[], int n1, int n2, int offset);
int main(void)
{
int arr[SIZE] = { 0, 1 };
printf("%d\n", arr[0]);
printf("%d\n", arr[1]);
sum(arr, 0, 1, 2);
return 0;
}
void sum(int arr[], int n1, int n2, int offset)
{
if (offset > SIZE)
return;
arr[offset] = arr[n1] arr[n2];
printf("%d\n", arr[offset]);
sum(arr, n1 1, n2 1, offset 1);
}
CodePudding user response:
Look at this check:
if (offset > SIZE)
return;
That means if offset is equal to SIZE, it passes.
arr[offset] with offset being equal to SIZE refers to the 41 nth element.
This array only have 40 element, hence the corruption.
If you run your program in a debugger, it should stop at the crash and you would be able to look at the value of offset that caused the crash.
A solution for this would be to change the check to if (offset >= SIZE).
