Global variables, local variables, static global variables, static local variables

Source: Internet
Author: User

Original: http://www.cnblogs.com/chenglei/archive/2009/09/06/1561367.html

Viewing from scopes:

Global variables have global scope. Global variables can be used for all source files only if they are defined in one source file. Of course, other source files that do not include global variable definitions need to declare the global variable again with the extern keyword.

A static local variable has a local scope. It is only initialized once, since the first initialization until the program and your new cabinet is over, he differs from global variables in that global variables are visible to all functions, while static local variables are always visible only to the function body that defines itself.

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

Static global variables also have global scope, and he differs from global variables in that if a program contains more than one file, the file he is using to define it does not work in other files, that is, a variable modified by the static keyword has a file scope. So even if two different source files have the same static global variables defined, they are also different variables.

From the allocated memory space:

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

The global variable itself is the static storage mode, static global variables are of course also static storage mode. The two are no different in the way they are stored. The difference is that the scope of a 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, that is, it is only valid within the source file that defines the variable, and it 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 only be common to functions within that source file, so it is possible to avoid causing errors in other source files.

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

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

From the above analysis, we can see that changing the local variable to a static variable is changing his storage mode, that is, changing his life time. Changing the global variable to a static variable changes his scope and limits his use, so the static specifier works differently in different places.

TIPS:

1. If the global variable is accessed only in a single file, it can be said that the variable is modified to a static global variable.

2. If the global variable is used only in a single function, you can modify the variable to a static local variable of that function.

3, global variables, static local variables, static global variables are stored in the static data store.

4, the function must use the case of a static variable: when the return value of a function is a pointer type, you must be the address of a static local variable as the return value, if the auto type, then return to the wrong pointer.

(Cannot return a pointer or reference to a local auto type)

Original: http://blog.chinaunix.net/uid-27411029-id-3497902.html

What is the essential difference between the objects stored in the static region and those stored in the stack area, and why the static variables stored in the static zone can return their addresses, while the automatic variables stored in the stack area cannot return their addresses.

Mainly because of their different management mechanisms, the lifetime of an object stored in a static region is the lifetime of the primary function, and the object's lifetime stored in the stack area is when the pointer function begins to run to the end of the pointer function, and the object's life cycle is ended when the pointer function ends and the address becomes an invalid address. Stack space is automatically allocated and freed by the compiler, and its stack space frees up memory at the end of the function. Heap areas are generally controlled by programmers to control their lifetimes. Therefore, the pointer function returns pointers that can point to a variable in a static region and cannot point to an automatic local variable.

When a function uses a pointer as a return value, it can point to the address of a static region, either to the address of the heap memory or to the stack space of the function caller, but it cannot point to the address of a function's internal stack memory.

Therefore, it is not possible to return the local pointer variable, not the type and nature of the pointer variable (not that the pointer is a local pointer variable), but the type and nature of the object that the pointer is pointing to. If the pointer points to a stack space inside a function, the program is illegal, and if it points to the address of a static region, it is legal.

Therefore, to determine whether the return value of a pointer function is legitimate, you should first look at the storage area of the object that the pointer variable points to, that is, the area to which the pointer is pointing. The nature of the objects in different regions depends on the validity of their life cycle, judging whether the returned pointer value is valid or not, the most essential thing is to see whether the life cycle of the object that the pointer points to is valid after the function ends. If the life cycle of the object is longer than the lifetime of the pointer function, the pointer returns a valid value, otherwise the pointer's value is an illegal address. The return address of the function is illegal even if the pointer points to the address of the heap area but the heap has been disposed at the end of the pointer function.

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.