Compare the differences between global variables, global static variables, local variables, local static variables

Source: Internet
Author: User
Tags variable scope

Compare the differences between global variables, global static variables, local variables, local static variables, where they are stored after compilation, where the initialized values are, when the memory is allocated, what effects the initial value has on these variables, and so on. To understand these problems, first of all to understand the following several points of knowledge.

The C language is divided into the following storage areas:

1. Stack (stack) is automatically allocated by the compiler when needed, and automatically clears the variable storage area when it is not needed. The variables that are usually stored are the function's parameter values, the values of local variables, etc., which are manipulated in a way similar to stacks in data structures.

2, heap area (heap) generally by the programmer to allocate the release, and the compiler completely no relationship, directly by our application to control, the general allocation of a piece of memory corresponding to a collection of memory. If the programmer does not release it, the operating system will automatically recycle after the program finishes.

3, Global (static storage) The storage of global variables and static variables is placed in a piece of memory, and global variables are divided into initialized and uninitialized. The initialized global variables and static variables are stored in an area, with uninitialized global variables and uninitialized static variables in another contiguous area. Released by the system at the end of the program run.

4, the constant storage area This is a special storage area, he holds constants, not allowed to modify the constant.

5. The program code segment holds the binary code of the function body.

There are several data segments in memory where data is stored:

1. BSS is the abbreviation of English block started by symbol, usually refers to a memory area that is used to store uninitialized global variables in the program, which is cleared by the kernel 0 when the program is loaded. BSS segments belong to static memory allocations. Its initial value is also determined by the user's own definition of the link location file, the user should be defined in the writable Ram area, the source program using malloc allocated memory is this piece, it is not based on the data size determined, mainly by the program in the allocation of memory maximum value is determined, but if the scope is exceeded, That is, the allocation fails and can be allocated after the space is freed.

2, the text paragraph is the program code snippet, in the At91 library is to represent the size of the program segment, which is automatically calculated by the compiler when the connection is compiled, when you place the symbol in the link location file in the code snippet, then the symbol represents the value of the code snippet size, compile the connection, The value represented by the symbol is automatically taken into the source program.

3, data contains static initialization, so the global variable with the initial value and the static variable in the data area. The starting position of the segment is also determined by the connection location file, the size is automatically assigned when the connection is compiled, it has nothing to do with the size of your program, but is related to the number of global variables, constants used by the program.

4. Stack saves the local variables and parameters of the function. is a "LIFO" (last in first Out,lifo) data structure, which means that the data that is finally placed on the stack will be the first data to be moved from the stack. The LIFO data structure is ideal for temporarily stored information, and for information that does not need to be saved for a long time. When a function or procedure is called, the system usually clears the stored local variables, function call information, and other information on the stack. Another important feature of the stack is that its address space is "down", which means that the more data is saved on the stack, the lower the stack's address. The top of the stack is at the end of the writable Ram area.

5, the heap Save function internal dynamic allocation of memory, is another to save the program information data structure, more accurate is to save the program's dynamic variables. A heap is a data structure in first Out,fifo. It only allows data to be inserted at one end of the heap, and data is moved at the other end. The heap's address space "increases up", which means that the heap's address is higher when more data is saved on the heap.

Variables can be divided into global variables, static global variables, static local variables, and local variables.

By storage: Global variables, static global variables, and static local variables are stored in the global data area of memory, and local variables are stored in the memory stack area.

By scope: The global variable is valid throughout the project file; a static global variable is only valid within the file that defines it; a static local variable is only valid within the function that defines it, except that the program allocates only one memory, and the variable does not disappear after the function returns; The local variable is valid within the function that defines it, but the function returns.

Global variables and static variables are initialized to 0 by the compiler if they are not manually initialized. The value of the local variable is not known. Static variables and all variables are completely different.

Static variables are relative to automatic variables and are called static variables, which means that static variables are not destroyed as program scope changes, but access to static variables is restricted by scope. Automatic variables are allocated in the function call stack, so the automatic variable cannot continue to hold the original value as the function exits. While static variables are allocated in the heap, the compiler initializes the static variables, and the static variables have only one in the entire program, unlike the automatic variables, which have multiple copies depending on the function call. The value of a static variable does not change after the function exits.

A global variable refers to a variable's scope is globally visible, and the variable scope is determined by the location of the variable's life. This means that variables declared outside of all curly braces are both global variables.

The difference between the various variables.

From the scope view:

Global variables and local variables: the difference between global and local variables is mainly due to the existence of different periods, the global variables are visible throughout the program generation period, local variables are visible in their scope. The memory allocation of a global variable is static, and if no initial value is assigned, it is initialized to 0. The memory allocations for local variables are dynamic, in the stack, and if not initialized, the initial values are dependent on the current memory value.

Global variables have global scope. A global variable can be used for all source files simply by defining it in one source file. Of course, other source files that do not include the definition of a global variable need to be declared with the extern keyword again for this global variable.

The static global variable also has global scope, the difference between him and the global variable is that if the program contains more than one file, he is used to define it in the file, does not work in other files, that is modified by the static keyword variable has a file scope. This way, even if two different source files define the same static global variables, they are also different variables.

Local variables are also only local scope, he is an automatic object, he does not exist during the program run, but only during the execution of the function, the function of the end of a call, the variable is revoked, the memory occupied by the recovery.

Static local variables have local scope. It is initialized only once, and since the first initialization until the end of the program has been present, the difference between the global variables and the globals is that the global variables are visible to all functions, whereas static local variables are always visible to the body of the function that defines itself.

int Fun (int  i) {    staticint a = i;     return A;}  for (int1; + +i) {    << fun (i) << Endl;   The output is always 1}    

See from memory allocation:

Global variables, static local variables, and static global variables all allocate space in the static store, while local variables allocate space on the stack.

Global variables themselves are static storage, and static global variables are, of course, static storage methods. The two are no different in how they are stored. The difference is that the scope of the non-static global variable is the entire source program, and 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.

1, static variables will be placed in the program's static data store, so that the next call can also maintain the original assignment. This is the difference between a stack variable and a heap variable.

2, the variable with static tells the compiler, itself only in the scope of the variable is visible. This is the difference between him and the global variable.

From the above analysis, it can be seen that changing the local variable to a static variable changes his storage mode, that is to change his life time. Changing a global variable to a static variable changes his scope and limits his scope of use, so that the static specifier plays a different role in different places.

The location of different types of variables in memory:

1. The initialized global variable is stored with data segment; The uninitialized global variable is stored with the BSS data segment.

2. Static global variable storage and data segment

3. Local variables are stored on the stack.

4, static local variables, not when the function is called when the allocation function is returned, but as a global variable is statically allocated, storing the data segment, but its scope in the function.

Compare the differences between global variables, global static variables, local variables, local static variables

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.