Search code examples
cgccbusbus-error

Bus error - What's wrong with this piece of code?


long int d[500], i;
d[1] = 1;
d[2] = 2;
d[3] = 4;
for(i = 4; i<=500; i++)
    d[i] = d[i-1] + d[i-2] + d[i-3];

int n = 500;
printf("%ld\n", d[500]);

The compiler is gcc. Bus error occurred at compiling. What caused this to happen?


Solution

  • long int d[500] declares an array with 500 items indexed from 0 to 499

    d[500] is outside the bounds of your array.