original address: c language static usage summary (turn) Author:
Xinruzhishui painful lesson:
assume test.h in defines a static bool g_test = false;
if test1.c and test2.c contain test.h, then test1.c and test2.c were generated two g_test, in the test1.c
set g_test = true, and test2.c still has not changed is false! shit! !
a, c program memory space layout
C program has the following components:
1) the text segment – CPU machine instructions executed part; only one copy of a program; read-only, to prevent accidents and to modify the program as self-instruction;
2) initialized data segment (data segment) – all in the program assigned the initial value of global variables, stored in here.
3) non-initialized data section (bss segment) – in the program did not initialize global variables; kernel is initialized to 0 to this section.
4) stack – growth direction: top-down growth; automatic variables, and each function call when the need to preserve information (return address; environmental information).
5) heap – dynamic storage points.
|———–|< br />

|———- -

stack

|———–|< br />

/

< br />

/

|———–|< br />

heap

— ——–|< br />
uninitialized
|———–|< br />
initialization
|———–|< br />
text segment
- ———-|< br /> Second, process-oriented programming in the static
1.
global static variable before the variable in the global keyword static, global variables is defined as a global static variable.
1) the location in memory: the static storage area (the static storage area in the entire program is running there)
2) Initialization: not initialized global static variables will be the beginning of the program automatically beginning to zero (the value of automatic object is arbitrary, unless he is shown initialized)
3) Scope: Global static variable in a statement outside his file is not visible. Precisely, define the place from the beginning to the end of the file.
see on the scope of the following procedures:
/ / teststatic1.c
void display ();
extern int n;
int main ()
{
n = 20;
printf (“% dn”, n) ;
display ();
return 0;
}

/ / teststatic2.c
static int n;
/ / define global static variables automatically initialized to 0, only in this document can be seen
void display ()
{
n ;
printf (“% dn”, n);
}
files were compiled, but the link when teststatic2.c variable n can not find the definition of an error.

define the benefits of a global static variable:
<1> not be accessed by other files, modify
<2> other files can use the same name variable, not conflict.
2.
local static variable before the variable in the local keyword static, local variables are defined as a local static variable.
1) the location in memory: the static storage area
2) Initialization: not initialized global static variables are automatically initialized to 0 (automatic object value is arbitrary unless he is shown initialized)
3) Scope: Scope is still the local scope, as defined in its function or the end of the block, scope come to an end.
Note: When the static local variables, when used to modify it to change the local variables are stored, kept from the original stack into static storage area. But the local static variable out of scope, and did not destroyed, but still reside in the memory of them, until the end of the process, but we can not visit him.
When the static global variable is used to modify the time, it changed the scope of global variables (in a statement that he is not visible outside the file), but it does not change the storage location, or In the static storage area.
3.
static function return type in function of the former with the keyword static, the function is defined as static functions.
function definitions and extern declarations by default, but only in a static function declaration in which he can see the files can not be other documents.
For example:
/ / teststatic1.c
void display ();
static void staticdis ();
int main ()
{
display ();
staticdis ();
renturn 0;
}

/ / teststatic2.c
void display ()
{< br />
staticdis ();
printf (“display () has been called n”);
}
< br /> static void staticdis ()
{
printf (“staticDis () has been calledn”);
}

files were compiled, but could not find function when connected staticdis () is defined, an error.
fact has not been compiled, vc2003 report teststatic1.c staticdis static function declared but not defined; by imjacob
static function defined benefits:
<1> Other the same file name can be defined in a function, not conflict
<2> static function can not be other documents.

storage specifier auto, register, extern, static, corresponding to the two storage period: automatic and static storage storage period period.

auto and register corresponding to the automatic storage period. Variables with automatic storage period of the variable into the statement when the block is created, it exists when the block activity, when to exit the block removed.
keyword extern and static have static storage term used to describe the variables and functions. Use a static local variable declared with static storage duration (static
storage duration), or static range (static
extent). Although his value remains in effect between function calls, but the name of the visibility is still limited in their local area. Static local objects in the program execution to the statement at the time the object was first initialized.
As the static variables, these features can achieve some specific functions.
1. Statistics the number of function
declare a local variable function, and set to static type, as a counter, so that the function is called each time when you can count. This is the number of statistical functions to be called the best approach, because this variable is closely related and function, and function may be called several different places, so from the perspective of the caller statistical comparisons difficult. Code is as follows:
void count ();
int main ()
{
int i;
for (i = 1; i <= 3; i )
count ();
return 0;
}
void count ()
{
static num = 0;
num ;
< br /> printf (“I have been called
% d”, num, “timesn”);
}
output is:
I have been called 1 times.
I have been called 2 times.
I have been called 3 times.