[Reserved] Audio Bible C language static Analysis
original address: strong> Audio Bible C language static Analysis Author: strong> walker 1, Overview 2), tells the compiler to use static variables, their only role in the variable range can be seen. This is the difference between it and global variables. unsigned int sum_int (unsigned int base) answer and analysis:
static variables declared in the C language has two features:
1), the variable will be placed in the program global memory, so you can call the next time you can maintain the original assignment. This is the stack variables and heap it with the difference between variables.
2, issue: Static understanding
on static variables, select all of the following statements is correct content:
A, only if the global variable access in a single C file, you can modify this variable as a static global variable, in order to reduce coupling between modules;
B, if the global variable is only accessed by a single function, you can set this variable to the function of the static local variables, in order to reduce coupling between modules;
C, dynamic design and use to access global variables, static global variables, static local variables of the function, need to consider re-entry the problem;
D, excessive static global variable, it can lead to stack overflow.
answer and analysis:
For A, B: overview of some of the instructions under this title b), we know, A, B are correct.
for C: an overview of some of the instructions under this title a), we know, C is correct (the so-called problem of re-entry function, described in more detail below).
For the D: static variables in the program global data area, rather than stack allocation, it is impossible to cause a stack overflow, D is wrong.
So the answer is A, B, C.
3, problem: non-reentrant functions
was designed as a function in the code view when they were reminded bug, because this function is not reentrant, Why?
{
unsigned int index;
static unsigned int sum = 0; / / Note that the static type of.
for (index = 1; index <= base; index )
{
sum = index;
}
return sum;
}
TD>
TR>
TBODY>
TABLE>
The so-called function is reentrant (can also be said to be predictable), namely: the same as long as the input data should produce the same output.
The reason why this function is unpredictable, because the function uses the static variables, static variables because of the characteristics of this function is called: with the “internal memory” function of the function. So if we need a reentrant function, then we must avoid the use of static variables in functions, this function of the static variable, use the principle is to not try to do.
modify the above function can be reentrant functions is very simple, as long as the sum variable is declared in the static keyword removed, the variable sum will turn into a type of auto variable, a function that becomes A reentrant function.
Of course, sometimes the function is necessary to use static variables, such as when a function returns a value of a pointer type, it must be static local variable address as the return value, if to auto type, then return for the wrong pointer.
