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


Hello Ankit,

On Fri, Sep 24, 2004 at 11:57:08AM +0100, Ankit Jain wrote:
> 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

There is no "problem" per se. It is just that stack space
and memory itself in your computer is limited. 

This has nothing (primarily) to do with the amount of main 
memory you got, rather each process has a limited (but adjustable) 
amount of stack space.

You're trying to put an array of 8*1450^2 bytes = 16MB on
the stack.

E.g. on my RH system the stack is by default limited to 8192KB:

$ ulimit -s
8192
$ ./try2
Segmentation fault

So your program of course segfaults.

Just make sure that you got enough stack space:

$ ulimit -s 17000
$ ./try2
999.999000

ulimit is a Bash builtin function; if you're instead using
another shell you might need to run a different command.

BTW, I think that "static memory allocation" is not the right
phrase to describe what you did in your program, the allocation
happens "automatically" when your program enters the main
function and gets automatically destroyed when returning from
main. That's why I learned that these sorts of variables are
called "automatic variables" (surprise, surprise ;-)).

HTH
-- 
Claudio


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