2009年11月5日 星期四

static variable

http://c.ittoolbox.com/documents/difference-between-static-global-variable-12174


Difference Between Static & Global Variable

6/10/2002 By ITtoolbox Popular Q&A Team for ITtoolbox as adapted from CPP-L discussion group
Summary:
What is the difference between static & global variable in C++?
Full Article:
Disclaimer: Contents are not reviewed for correctness and are not endorsed or recommended by ITtoolbox or any vendor. Popular Q&A contents include summarized information from ITtoolbox CPP-L discussion unless otherwise noted.

1) Adapted from response by Smit on Fri, 7 Jun 2002
http://Groups.ITtoolbox.com/archives/archives.asp?l=cpp-l&i=101470

Variables defined local to a function disappear at the end of the function scope. So when we call the function again, storage for variables is created and
values are reinitialized. So if we want the value to be extent throughout the life of a program, we can define the local variable as "static." Initialization is performed only at the first call and data is retained between func calls.

Had it been gloal variable, it would have been available outside the scope of the function, but static variable is not available outside the scope of a function (helpful in localizing errors - as it can't be changed outside the func
scope).

2) Adapted from response by Meraj on Fri, 7 Jun 2002
http://Groups.ITtoolbox.com/archives/archives.asp?l=cpp-l&i=101471

Static and global variable differ a lot in their behaviour to life and scope. First, let me distinguish between life and scope. Life of an object determines whether the object is still in the memory (of the process) whereas scope of the object is whether can I know the variable by its name at this position. It is possible that object is live, but not visible (not in scope) but not that object is not alive but in scope (except for dynamically allocated objects where you refer object through pointers).

Static variables are local in scope to their module in which they are defined, but life is throughout the program. Say for a static variable inside a function cannot be called from outside the function (because it's not in scope) but is alive and exists in memory. The next time this function is entered (within the same program) the same chunk of memory would be accessed now retaining the variables old value and no new memory is allocated this time for this variable like other variables in the function (automatic variables). So basically the variable persists throughout the program. Similarly if a static variable is defined in a global space (say at beginning of file) then this variable will be
accessible only in this file (file scope).

On the other hand global variables have to be defined globally, persists (life is) throughout the program, scope is also throughout the program. This means such variables can be accessed from any function, any file of the program.

So if you have a global variable and u r distributing ur files as a library and you want others to not access your global variable, you may make it static by just prefixing keyword static (of course if same variable is not required in other files of yours).

沒有留言: