This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
RE: Static memory allocation
- From: "Huber, George K RDECOM CERDEC STCD SRI" <George dot K dot Huber at us dot army dot mil>
- To: "'Ankit Jain'" <ankitjain1580 at yahoo dot com>, gcc <gcc-help at gcc dot gnu dot org>, linux prg <linux-c-programming at vger dot kernel dot org>
- Date: Fri, 24 Sep 2004 10:21:25 -0400
- Subject: 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