linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Static memory allocation
@ 2004-09-24 10:57 Ankit Jain
  2004-09-24 12:51 ` Patrick Percot
  2004-09-25 10:03 ` Jon Masters
  0 siblings, 2 replies; 6+ messages in thread
From: Ankit Jain @ 2004-09-24 10:57 UTC (permalink / raw)
  To: gcc, linux prg

hi

well i had tried to enquire about this problem

i was not able to find the solution. i know it workds
when i declare the array globally. i know if i use
malloc it will work and i am using the same in my
program

but this question is again in my mind that what could
be the reason that it dosent work here on my system
because last time when i asked this question on some
sytem it was able to run which they said that they
have less emory then that of my system

my system config.: redhat linux 9.0 +512 RAM
this is what my terminal displays

[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]$

i just want to know that if there is some problem
related to stack how t oget rid of it

thanks

ANkit




________________________________________________________________________
Yahoo! Messenger - Communicate instantly..."Ping" 
your friends today! Download Messenger Now 
http://uk.messenger.yahoo.com/download/index.html

^ permalink raw reply	[flat|nested] 6+ messages in thread
* RE: Static memory allocation
@ 2004-09-24 14:21 Huber, George K RDECOM CERDEC STCD SRI
  0 siblings, 0 replies; 6+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2004-09-24 14:21 UTC (permalink / raw)
  To: 'Ankit Jain', gcc, linux prg

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2004-09-25 21:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-09-24 10:57 Static memory allocation Ankit Jain
2004-09-24 12:51 ` Patrick Percot
2004-09-25  9:14   ` Ankit Jain
2004-09-25 21:51     ` Patrick Percot
2004-09-25 10:03 ` Jon Masters
  -- strict thread matches above, loose matches on Subject: below --
2004-09-24 14:21 Huber, George K RDECOM CERDEC STCD SRI

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).