Path: chuka.playstation.co.uk!news From: sosman@terratron.com (Steven Osman) Newsgroups: scee.yaroze.beginners Subject: Re: static variables Date: Mon, 01 Nov 1999 02:55:17 GMT Organization: PlayStation Net Yaroze (SCEE) Lines: 38 Message-ID: <381e00cb.566387381@news.playstation.co.uk> References: <7vhr5g$4g31@chuka.playstation.co.uk> <7vi161$a6n2@chuka.playstation.co.uk> <01bf23f9$82197ac0$429124c3@pal-s-omnibook> NNTP-Posting-Host: 209.27.57.69 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Newsreader: Forte Agent 1.5/32.452 Also, and quite importantly... local variables are allocated as the function is called. That means, when you are not in the function, the memory is not taken up! If you have a non-recursive application, you can't just make everything static for speed, without considering that you will be consuming a lot of memory, given enough variables. Steven On 31 Oct 1999 23:57:47 GMT, "pal" hotmail.com> wrote: >Nathan Miller wrote : > >>I've recently been reading about using 'static' variables to 'move >>them off the stack' but I'm lost as to what it means. > > >Variables declared in a function body are allocated on the stack when the >function is entered, unless you specify otherwise. If you wish so, you can >force them to go in registers ("register" modifier) for faster access, or >to be global ("static" modifier) so that they're allocated once only before >the program starts. > >When a variable is declared "static", it behaves exactly as if it were >declared outside the function (scope aside). There's no more need for >allocating it on the stack every time the function is entered - so >execution time is gained. Be aware however that the function must not be >called again before it ends (well, technically it can, but you must manage >to avoid it corrupting its static variables) - that means, no recursion and >no parallelism. Be careful with initialisation too. > > > >pal