An analysis of the static of C language

Source: Internet
Author: User

1. Overview

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.

2. Question: 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.

3. 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, why?

unsigned int sum_int( unsigned int base )
{
unsigned int index;
static unsigned int sum = 0; // 注意,是static类型的。
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.

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.