C-language static variables and static functions

Source: Internet
Author: User

Static C language

The C language program can be seen as a series of external objects, which may be variables or functions. The internal variable refers to the function parameters and variables defined within the function. External variables are defined outside of functions, so they can be used in many functions. Because the C language does not allow other functions to be defined in one function, the function itself can only be "external."
Because the C language code is organized in files, an external variable or function can only be defined once in a file in all source files of a source program. Other files can be accessed through an extern declaration (a source file that defines an external variable or function can also contain an extern declaration for that external variable).
Static allows you to qualify variables or functions as statically stored. If you qualify external variables and functions with static, you can limit the scope of the object to the remainder of the compiled source file. By using static to qualify external objects, you can achieve the purpose of hiding external objects. Thus, a static-qualified variable or function does not conflict with the same name in other files in the same program. If you qualify an internal variable with static, the variable has memory from the beginning of the program and is not allocated and disappears with the invocation and exit of its function.
the benefit of using static functions in C language : Static functions are automatically assigned to a storage area that is always in use until the application instance is exited, avoiding the call function when the stack is pushed out, much faster. The keyword "static", translated into Chinese is "still", so the internal function is also called static function. But the meaning of "static" here is not the way of storage, but the scope of the function is limited to this file. The advantage of using internal functions is that when different people write different functions, they do not have to worry about the functions they define, whether they will have the same name as the functions in other files, because the same name does not matter.

the semantics of static in C language
1.static variable:
1). Local
A. A static local variable is defined within a function, and the lifetime is the entire source program, but the scope is the same as an automatic variable and can only be used within the function that defines the variable. After exiting the function, it cannot be used, although it continues to exist.
B. For static local variables of the basic type if the initial value is not assigned in the description, the system automatically assigns a value of 0. For an automatic variable, the value is indeterminate.
2). Global
The global variable itself is the static storage mode, static global variables are of course also static storage mode. But their scope, the scope of a non-static global variable is the entire source program (multiple source files can be used together), while a static global variable restricts its scope, which is only valid within the source file that defines the variable, and cannot be used in other source files of the same source program.
2.static function (also called intrinsic function)
Can only be called by functions in this file, not by functions in other files of the same program. Different from general non-static functions (external functions)
Static in C can be used to modify variables, or to modify functions.
First look at the time to modify the variable. Variables in C can be divided into the existence of global data areas, stacks and heaps. In fact, we usually say the stack is stack and does not contain the right, do not confuse.
int A;
Main ()
{
int b;
int c* = (int *) malloc (sizeof (int));
}
A is a global variable, B is a stack variable, and C is a heap variable.
Static the modification of a global variable can be considered to limit the only reference to this variable in this file. Some programs are composed of a lot of. c files. Variables can be referenced to each other, but after the static adornment is added, this variable can only be referenced by functions in this file.
Static on a stack variable, you can think of the life cycle of the stack variable extended to the end of program execution. In general, the life cycle of the stack variable is managed by the OS, and the life of the stack variable is over when the stack is retired. But after adding the static modifier, the variable is not stored on the stack, but is stored with the global variable. At the same time, it cannot be used after you leave the function that defines it, but if you call the function that defines it again, it can continue to work and save the value left after the previous call.
A static modification of a function is similar to that of a global variable, and can only be called by a function in this file, not by a function in the other file of the same program.
A variable of a static declaration has two characteristics in the C language:
1), the variable will be placed in the program's global storage area, so that the next time the call can also maintain the original assignment. This is how it differs from stack variables and heap variables.
2, variable with static to tell the compiler, itself only in the scope of the variable to be visible. This is the difference between it and the global variable.

Question: a static understanding

For static variables, select all of the following statements that are correct:

A, if the global variable only in a single C file access, you can modify this variable to static global variables, to reduce the coupling between modules;

B, if the global variable is only accessed by a single function, this variable can be changed to the static local variable of the function to reduce the coupling between the modules;

C, the design and use of access to dynamic global variables, static global variables, static local variables of the functions, you need to consider the problem of reentrant;

D, static global variable is too large, but that can cause stack overflow.

Answer and Analysis:

For a,b: According to the description in this section B), we know that a,b is correct.

For C: According to the description in this overview section a), we know that C is correct (the so-called function reentrant problem, described below in detail).

For d: Static variables are placed in the program's global data area, rather than on the stack, so it is not possible to cause a stack overflow and D is wrong.

So the answer is a, B, C.

Problem: Non-reentrant function

The following function has been designed to be alerted to bugs in the Code view because the function is not reentrant and why.

unsigned int sum_int (unsigned int base)
{
unsigned int index;
static unsigned int sum = 0; Note that the static type is.
for (index = 1; index <= base; index++)
{
sum + = index;
}
return sum;
}

Answer and Analysis:

The so-called function is reentrant (or predictable), that is, the same output should be produced as long as the input data is the same.
This function is unpredictable because the static variable is used in the function, because of the characteristics of the static variable, such functions are called: Functions with "internal memory" function. So if we need a reentrant function, then we have to avoid using the static variable in the function, the static variable in the function, and the principle is that you don't have to try to use it.
Modifying the above function to a reentrant function is simple, as long as you remove the static keyword from the sum variable, the sum of the variables becomes a variable of type auto, and the function becomes a reentrant function.
Of course, there are times when you have to use a static variable in a function, such as when a function's return value is a pointer type, it must be the address of a static local variable as the return value and, if it is the auto type, return to the wrong pointer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.