original address: strong> Audio Bible C language static Analysis Author: strong> walker
1, Overview
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), 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.
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 sum_int (unsigned int base) { 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>
answer and analysis: 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.
original address: strong> c language static usage summary (turn) Author: strong> Xinruzhishui painful lesson: assume test.h in defines a static bool g_test = false; if test1.c and test2.c contain test.h, then test1.c and test2.c were generated two g_test, in the test1.c set g_test = true, and test2.c still has not changed is false! shit! ! a, c program memory space layout C program has the following components: 1) the text segment – CPU machine instructions executed part; only one copy of a program; read-only, to prevent accidents and to modify the program as self-instruction; 2) initialized data segment (data segment) – all in the program assigned the initial value of global variables, stored in here. 3) non-initialized data section (bss segment) – in the program did not initialize global variables; kernel is initialized to 0 to this section. 4) stack – growth direction: top-down growth; automatic variables, and each function call when the need to preserve information (return address; environmental information). 5) heap – dynamic storage points. |———–|< br />
|———- -
stack
|———–|< br />
/
< br />
/
|———–|< br />
heap
— ——–|< br /> uninitialized |———–|< br /> initialization |———–|< br /> text segment - ———-|< br /> Second, process-oriented programming in the static 1. global static variable before the variable in the global keyword static, global variables is defined as a global static variable. 1) the location in memory: the static storage area (the static storage area in the entire program is running there) 2) Initialization: not initialized global static variables will be the beginning of the program automatically beginning to zero (the value of automatic object is arbitrary, unless he is shown initialized) 3) Scope: Global static variable in a statement outside his file is not visible. Precisely, define the place from the beginning to the end of the file. see on the scope of the following procedures: / / teststatic1.c void display (); extern int n; int main () { n = 20; printf (“% dn”, n) ; display (); return 0; }
/ / teststatic2.c static int n; / / define global static variables automatically initialized to 0, only in this document can be seen void display () { n ; printf (“% dn”, n); } files were compiled, but the link when teststatic2.c variable n can not find the definition of an error.
define the benefits of a global static variable: <1> not be accessed by other files, modify <2> other files can use the same name variable, not conflict. 2. local static variable before the variable in the local keyword static, local variables are defined as a local static variable. 1) the location in memory: the static storage area 2) Initialization: not initialized global static variables are automatically initialized to 0 (automatic object value is arbitrary unless he is shown initialized) 3) Scope: Scope is still the local scope, as defined in its function or the end of the block, scope come to an end. Note: When the static local variables, when used to modify it to change the local variables are stored, kept from the original stack into static storage area. But the local static variable out of scope, and did not destroyed, but still reside in the memory of them, until the end of the process, but we can not visit him. When the static global variable is used to modify the time, it changed the scope of global variables (in a statement that he is not visible outside the file), but it does not change the storage location, or In the static storage area. 3. static function return type in function of the former with the keyword static, the function is defined as static functions. function definitions and extern declarations by default, but only in a static function declaration in which he can see the files can not be other documents. For example: / / teststatic1.c void display (); static void staticdis (); int main () { display (); staticdis (); renturn 0; }
/ / teststatic2.c void display () {< br /> staticdis (); printf (“display () has been called n”); } < br /> static void staticdis () { printf (“staticDis () has been calledn”); }
files were compiled, but could not find function when connected staticdis () is defined, an error. fact has not been compiled, vc2003 report teststatic1.c staticdis static function declared but not defined; by imjacob static function defined benefits: <1> Other the same file name can be defined in a function, not conflict <2> static function can not be other documents.
storage specifier auto, register, extern, static, corresponding to the two storage period: automatic and static storage storage period period.
auto and register corresponding to the automatic storage period. Variables with automatic storage period of the variable into the statement when the block is created, it exists when the block activity, when to exit the block removed. keyword extern and static have static storage term used to describe the variables and functions. Use a static local variable declared with static storage duration (static storage duration), or static range (static extent). Although his value remains in effect between function calls, but the name of the visibility is still limited in their local area. Static local objects in the program execution to the statement at the time the object was first initialized. As the static variables, these features can achieve some specific functions. 1. Statistics the number of function declare a local variable function, and set to static type, as a counter, so that the function is called each time when you can count. This is the number of statistical functions to be called the best approach, because this variable is closely related and function, and function may be called several different places, so from the perspective of the caller statistical comparisons difficult. Code is as follows: void count (); int main () { int i; for (i = 1; i <= 3; i ) count (); return 0; } void count () { static num = 0; num ; < br /> printf (“I have been called % d”, num, “timesn”); } output is: I have been called 1 times. I have been called 2 times. I have been called 3 times.
May 15, 2010
Location: AQSIQ> Public Administration> Council make announcements> General Announcements> 2009 2009 年 strong> General Administration of Quality Supervision, Inspection and Quarantine, “a moratorium on imports of the United States by the Salmonella typhimurium contamination of peanut butter and downstream products announcement, “No. 10 of 2009 No. 10 of 2009 to suspend imports of U.S. on Salmonella typhimurium contamination by peanut butter and downstream product announcements Total Quality Supervision, Inspection and Quarantine Board “on suspension of imports of the United States by the Salmonella typhimurium contamination of peanut butter and downstream products, announced,” No. 10 of 2009 strong> 2009 No. 10 moratorium on imports of U.S. contaminated by Salmonella typhimurium Peanut Butter and downstream product announcements U.S. FDA recently announced that the U.S. Peanut Co., Ltd. (Peanut Corporation of America) at a Georgia peanut butter factory contaminated by Salmonella typhimurium, and result in some downstream components containing peanut butter product contamination. At present, 43 states reported 474 cases related and may have caused six deaths. United States has 11 companies are recalling contaminated and potentially contaminated peanut butter and its downstream products. U.S. FDA warns consumers not to eat recalled food. for the protection of our consumers health, according to the “Import and Export Commodity Inspection Law” and “Food Sanitation Law” requirement, now announced as follows: First, from now on, prohibit the following product recall is being implemented U.S. production of peanut butter and peanut butter ingredients imported. 1, Peanut Corporation of America 2, Ralcorp Frozen Bakery Products, Inc. 3, South Bend Chocolate Company 4, McKee Foods Corporation 5, Hy- Vee Inc. 6, Perry Ice Cream 7, Kellogg Company 8, King nut Companies 9, Kroger Co. 10, Abbot Nutrition 11, Meijer Second, the entry-exit inspection and quarantine authorities should strengthen from the United States with peanut butter and peanut butter ingredients food inspection and quarantine, released after passing immigration and strengthen entry mail items, goods carried by passengers inspection work, non of the 11 companies related products by mail, passengers, etc. entry. Once found, immediately in accordance with relevant regulations, the situation promptly reported to the State Administration of Quality Supervision. Third, the importer must immediately stop selling the 11 companies related products have been sold contaminated and potentially contaminated batch of the product recall notice immediately (U.S. firms recall product information available from the following website: http://www.fda.gov/oc/opacom/hottopics/salmonellatyph.html # recalls); advises consumers not to purchase and use of the 11 companies related products. view of the U.S. investigation is still underway, the scope of the contaminated product is likely to continue to expand, since the implementation of all U.S. FDA announced the recall of products related products according to company regulations of this announcement. January 24, 2009 the affected companies, Amway ranked first. Amway friends should have awakened it. Do not always feel that something of their own country than others, and we are all Chinese.
|