Transferred from: http://www.cnblogs.com/lzjsky/archive/2010/11/19/1882064.html
A static global variable is formed by the description of the global variable (external variable), preceded by static. Global variables themselves are static storage, and static global variables are, of course, static storage methods. The two are not different in how they are stored. The difference between the two is that the scope of the non-static global variable is the whole source program, when a source program consists of multiple source files, non-static global variables are valid in each source file. A static global variable restricts its scope, which is valid only within the source file that defines the variable, and cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file, it can be common only for functions within that source file, so you avoid causing errors in other source files.
From the above analysis, it can be seen that changing the local variable to a static variable changes its storage mode, which changes its life time. Changing a global variable to a static variable changes its scope and limits its scope of use.
The static function differs from the normal function scope. Only in this document. Functions that are used only in the current source file should be described as intrinsic (static) and intrinsic functions should be described and defined in the current source file. For functions that can be used outside of the current source file, it should be stated in a header file that the source file to which the function is to be used is to contain the header file
What is the difference between a static global variable and a normal global variable: the static global variable is only initialized once, preventing it from being referenced in other file units;
What is the difference between a static local variable and a normal local variable: the static local variable is initialized only once, the next time based on the last result value;
What is the difference between a static function and a normal function: The static function has only one copy in memory, and the normal function maintains a copy of each call
The local variables of the program exist in (the stack), the global variables exist in the (static zone), and the dynamic requisition data exists in the (heap).
The lifetime of the extern global variable, the static global variable, and the static local variable is "permanent", except that the visible domain differs. The extern global variable visible area is a project, the static global variable visible area is a file, and the visible area of the static local variable is a block.
From a code maintenance perspective, modifications to the extern variable may affect all code, and modifications to the static global variable may affect the code in one file, and modifications to the static variable may affect the code of a block, so the priority is the static partial when the variable type is selected > Static global >extern Global. But they have common drawbacks: functions that use these types of variables will be non-reentrant, not thread-safe. There are many functions in the C + + standard library that use static local variables, and the current implementation provides them with two sets of code, a single-threaded version using a static variable and a multithreaded version that uses "Thread global variables," such as Rand,strtok.
A process free memory space of 4G, can be divided into the storage of static data, code, system memory, heap, stack and so on: the activity records generally hold the call parameters, return address and so on. The biggest difference between heaps and stacks is that the heap is allocated memory from a low address to a high address, while the stack is from high to low. Global and static data is stored in the global data area, and the rest is in the stack, with the memory allocated by malloc or new in the heap. Generally the stack is at the low address, the heap is located high address
The difference between static and global and local variables