Re: Use of static and extern

From: Francis Glassborow (francis_at_robinton.demon.co.uk)
Date: 06/03/04


Date: Thu, 3 Jun 2004 11:13:13 +0100

In article <94387a7d.0406022143.32eec035@posting.google.com>, Rodney B.
Elliott <rodney.elliott@canterbury.ac.nz> writes
>Am I right in thinking you get *all* the functionality *all* of the
>time? So for example if I have a static class member variable, it
>will reside in memory until the program terminates?

The basic commonality for all uses of static applied to variables
(rather than functions) is that the raw memory for the object is
'allocated' at program start-up. It is there throughout the life of the
program. Thinking this way can help understand its usage.

In the case of file-scope static's it is trivially true because all
forms of global object use memory allocated at program start-up. That
particular use of static is unfortunate (and goes back to the early days
of C) because it fixes a name visibility problem that should have been
dealt with by making all names invisible outside the file in which they
are declared unless they were explicitly 'exported'. Too late to fix
that now -- just mark all file-scope (but not namespace scope)
declarations as static until you know you want them otherwise.

The reason that function scope statics provide functions with state
(i.e. they remember information from one invocation to another) is
exactly because they are using 'static' memory (i.e. memory provided
uniquely at start-up). Likewise class scope statics work because they
provide a class with state independent of the existence of any
instances.

In both cases the variables work the way we want them to exactly because
of the way that their memory is provided. Incidentally that is the
origin of the keyword 'static' because static properties are those
things done at compile time, and it is the compiler that generates the
instructions for program start-up.

-- 
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


Relevant Pages

  • Re: Are static array pointers thread safe?
    ... array. ... a separate area of memory dedicated to them. ... globals, static data members of classes, and local statics. ... observe all sorts of strange things in the absence of synchronization, ...
    (microsoft.public.vc.mfc)
  • Re: storage locations for different data types
    ... the memory for all objects with static storage duration ... This is static storage. ... that const statics CAN be treated differently from non-const statics. ...
    (comp.lang.cpp)
  • Re: Static Variables
    ... but how does static variable takes place in memory?? ... to place them on a stack, (except if it's declared within the main ... In most implementation statics and other file scope objects are ...
    (comp.lang.c)
  • Re: copymemory basic question
    ... a pre-amble usually allocates all the stack ... dynamic memory on exit, is very significant. ... >> Dim locals? ... One could think that Statics are faster as they don't ...
    (microsoft.public.vb.winapi)