C language static

Source: Internet
Author: User
C Language

Static C Language

C-language programs can be viewed as composed of a series of external objects, which may be variables or functions. Internal variables refer to the function parameters and variables defined within the function. External variables are defined outside functions, so they can be used in many functions. Since the C language does not allow defining other functions in a function, the function itself can only be "external ".
C language code is organized in files. In all source files of a source program, an external variable or function can be defined only once in a file, other files can access it through the extern Declaration (the source file defining the external variable or function can also contain the extern Declaration for the external variable ).
Static storage can restrict variables or functions to static storage. If you use static to restrict external variables and functions, You can restrict the scope of the object to the rest of the compiled source file. You can use static to restrict external objects to hide them. Therefore, static variables or functions do not conflict with the same name in other files in the same program. If you use static to limit the internal variable, the variable will have memory from the beginning of the program and will not be allocated and disappear as the function calls and exits.
Benefits of using static functions in C Language:

  1. Static functions are automatically allocated to a used storage area until they exit the application instance. This avoids the pressure on the stack to exit the stack when calling the function, which is much faster.
  2. The keyword "static" is "static" in Chinese. Therefore, internal functions are also called static functions. However, the meaning of "static" here is not the storage method, 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, you don't have to worry about whether your own defined functions will have the same name as the functions in other files, because the same name does not matter.

Static semantics in C Language
1. Static variables:
1). Local
A. Static local variables are defined in the function. The lifetime is the entire source program, but the scope is the same as that of automatic variables. They can only be used in the function that defines the variable. After exiting the function, although the variable still exists, it cannot be used.
B. If an initial value is not assigned to a static local variable of the basic type, the system automatically assigns the value 0. If the initial value is not assigned to the automatic variable, the value is not fixed.
2). Global
Global variables are static storage, and static global variables are also static storage. But their scope: the scope of non-static global variables is the entire source program (multiple source files can be used together), while static global variables limit their scope, that is, it is valid only in the source file defining the variable and cannot be used in other source files of the same source program.
2. Static function (also called internal function)
It can only be called by functions in this file, but not by functions in other files of the same program. Different from general non-static functions (external functions)
Static can be used to modify variables or functions in C.
Let's first look at the time when it is used to modify variables. Variables in C can be divided into global data areas, stacks, and stacks. In fact, the stack we usually call is a stack that does not contain a pair. Don't confuse it.
Int;
Main ()
{
Int B;
Int C * = (int *) malloc (sizeof (INT ));
}
A is a global variable, B is a stack variable, and C is a stack variable.
Static modification to the global variable can be considered as limiting that the variable can only be referenced in this file. Some programs are composed of many. c files. Variables can be referenced by each other. However, after static modification, the variable can only be referenced by functions in this file.
Static modification of stack variables can be considered to extend the life cycle of stack variables to the end of program execution. In general, the life cycle of stack variables is managed by OS. During the rollback process, the life of stack variables ends. However, after static modification, the variables are not stored in the stack, but stored together with global variables. At the same time, it cannot be used after the function that defines it is left blank. However, if you call the function that defines it again, it can continue to be used and save the value left after the previous call.
Static Function Modification is similar to global variable modification. It can only be called by functions in this file, but cannot be called by functions in other files of the same program.
Static declared variables have two features in the C language:
1) The variables will be placed in the global storage area of the program, so that the original values can be maintained during the next call. This is the difference between stack variables and heap variables.
2) The variable uses static to notify the compiler that it is only visible within the scope of the variable. This is the difference between it and global variables.

Question: static understanding

For static variables, select the correct content for all the following statements:

A. if the global variable is only accessed in a single c file, you can change the variable to a static global variable to reduce the coupling between modules;

B. If the global variable is only accessed by a single function, you can change the variable to the static local variable of the function to reduce the coupling between modules;

C. When designing and using functions that access dynamic global variables, static global variables, and static local variables, you need to consider re-import;

D. The static global variable is too large, which may cause stack overflow.

Answer and analysis:

For A, B: according to the description in the overview section B), we know that a and B are correct.

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

For D: static variables are placed in the global data zone of the program, rather than allocated in the stack, so it is impossible to cause stack overflow. D is wrong.

Therefore, the answer is A, B, and C.

Problem: Non-reentrant Functions

I have designed a function such as the next function. I was reminded of a bug during code check because this function cannot be reentrant. Why?

Unsigned int sum_int (unsigned int Base)
{
Unsigned int index;
Static unsigned int sum = 0; // note that it is of the static type.
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 generated as long as the input data is the same.
This function is unpredictable because it uses static variables. Because of the characteristics of static variables, such a function is called a function with the "internal memory" function. Therefore, if we need a reentrant function, we must avoid using static variables in the function. The principle of using static variables in this function is that we do not need to use them as much as possible.
It is very easy to change the above function to a reentrant function. As long as the static keyword in the declared sum variable is removed, the variable sum is changed to an auto type variable, A function is a reentrant function.
Of course, in some cases, static variables must be used in functions. For example, when a function returns a pointer type, the address of a static local variable must be used as the return value, if it is of the auto type, an error pointer is returned.

Related Article

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.