Type and storage location of C language variables

Source: Internet
Author: User

1. C language variables are mainly divided into global variables, static global variables, local variables, static local variables and register variables. Where static variables are decorated with the static keyword. The memory that the program occupies can be divided into the following parts: (1) Code Snippet-Store program code, read-only, cannot be modified. (2) Global Zone (static zone), also known as data segment. where the. Data segment holds the initialized global variables and static variables (static global variables and static local variables). The BSS segment holds uninitialized global variables and static variables (static global variables and static local variables). The BSS segment populates the uninitialized variable with 0. (3Heap: is a linear structure, similar to a linked list implementation, can also be implemented by other methods. malloc, the REALLOC function typically allocates memory from the heap. Note: Different from the heap in the data structure. (4) Stack: Last-in-first-out structure. The main stores are local variables, function parameters, and function addresses. (5) Constant Area: stores string constants. 2. Variable lifetime and scope global variables: Global variables are scoped to the entire source program, which is valid in multiple files. Through the extern declaration. Static global variables: The storage domain of the static global variable and the global variable is the same and is stored in the data segment. However, the scope of the static global variable declared with static is limited to this file, but the scope is not the same. is not valid outside of the file. Static before a function or variable to prevent naming conflicts. Local variables: Local variables are defined within the function body, and are automatically freed when the function is executed. The scope is the entire function body. Static local variables: Unlike local variables, it is stored in the global or static zone, and local variables are stored on the stack. The scope of a static local variable is also the function body, which is invalid outside the function body. However, when the function is finished, the static local variable persists, and the next call can take advantage of the last saved value, that is, the static local variable is initialized 1 times and does not repeat initialization. If a static variable is declared in the header file, the address of the static variable in each source file that contains the header file is not the same, which is considered to be a different variable, which prevents the variable from being redefined. It also shows that a static global variable restricts the scope to a file. 3. You can view each segment by readelf-a the executable file.

Type and storage location of C language variables

Related Article

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.