Re: Use of static and extern
From: Francis Glassborow (francis_at_robinton.demon.co.uk)
Date: 06/03/04
- Next message: Chris \( Val \): "Re: Use of static and extern"
- Previous message: Andre Heinen: "Re: throw exception in destructor"
- In reply to: Rodney B. Elliott: "Re: Use of static and extern"
- Next in thread: Leor Zolman: "Re: Use of static and extern"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Chris \( Val \): "Re: Use of static and extern"
- Previous message: Andre Heinen: "Re: throw exception in destructor"
- In reply to: Rodney B. Elliott: "Re: Use of static and extern"
- Next in thread: Leor Zolman: "Re: Use of static and extern"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|