Static Analysis of Water Drop stone wearing C Language

Source: Internet
Author: User

1. Overview
Variables declared in static areC LanguageThere are two features:
1) The variable will be placed inProgramIn the global storage area, so that the original value 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.

2. Problem: 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 area 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.

3. problem: the function cannot be reloaded.

Once designed the next functionCodeA bug was prompted during the inspection because this function cannot be reinjected. Why?

 
UnsignedIntSum_int (unsignedInt Base)
{
UnsignedIntIndex;
StaticUnsignedIntSum =0;//Note: It is static.
For(Index =1; Index <=Base; Index ++)
{
Sum + = index;
}
ReturnSUM;
}

answer and analysis:
the so-called the 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 static variables are used in the function. Because of the characteristics of static variables, such functions are called: functions with "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 easy to change the above function to a reentrant function. if you remove the static keyword in the declared sum variable, the variable sum is changed to an auto 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 the static local variable must be used as the return value. If it is of the auto type, the return value is an error 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.