This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Static memory allocation


Ankit wrote:

>[ankit@Ankit fft]$ cat try2.c
>#include <stdio.h>
>int main()
>{
>  double a[1450][1450];
>  a[1450][0]=999.999;
>  printf("%lf\n",a[1450][0]);
>  return 0;
>}
>[ankit@Ankit fft]$ gcc try2.c
>[ankit@Ankit fft]$ ./a.out
>Segmentation fault
>[ankit@Ankit fft]$

This is a classic `off-by-one' error.  When you allocate an
array int a[10] you are asking for ten storage locations that 
are numbered 0 -- 9. (not 10).

In your program, the legal range for each index is 0-1449 (not 1450).

George


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]