Storage Time/Scope of the "programming language" variable and usage of Static/extern

Source: Internet
Author: User
Tags function prototype

Storage class:

Storage Time link Memory management

For a variable (a different storage type) it can be described by the storage period, the link property, and the corresponding scope.

The storage period is the time the variable is held in memory, and the scope and linkage of the variable indicate which parts of the program can use the variable name. So there's a non-pass storage period, link properties, and a combination of scopes

"1" Storage period (the lifetime of a variable that represents the storage and release time of a variable in the memory space)

Local variable register variable global variable

1 the life cycle of a local variable (an automatic variable) is allocated memory space within the code block if the variable is called, and the allocated memory is freed until the block of code is exited. This idea treats the memory used by the automatic variable as a reusable workspace or staging memory.

2 The general variables of the register variables are stored in memory, but there are also variables stored in CPU Register, or stored in the cache, as much as the block is accessed and manipulated, because a register variable exists in the register rather than in memory, all cannot get the address of the register variable, but in other respects, the register variable and the automatic variable, that is, They all have code block scopes, empty joins, and automatic storage periods, and register variables can be represented by Register .

3 Global variables (static external variables) It has file scopes, external links, and static storage periods, defining variables outside of all functions, creating an external global variable with static variables that are externally linked. That is, from the location of the declaration to the end of the file is visible. If the program is clearer, you can declare this variable with extern in front of the external variable , but we don't normally add extern To declare static variables (global variables) of external links defined in this file, but when you want to reference static variables that are externally linked to other files, you must add extern to declare them at this time.

Scope / Link Properties

External link internal link empty link

1) variables with code block scope or function prototype scope have NULL connections , which means that they are defined by the code block or the function prototype.

2) variables declared with extern have external link properties that indicate that the variable can be used anywhere in a file or multiple files

3) A variable declared with static has an internal link property that indicates that the variable can only be used anywhere in this file, where it is important to note that the static A declared variable can be a file-scoped variable (a global variable with an internal link) or a local static variable with a block of code, and their common feature is a static storage period, from a function until the next call, where the values are recorded in memory, and the variables do not disappear when they are finished working. But it is declared as a static variable with a code block scope it is an empty connection property

Memory management (where variables are stored in memory)(focus)

variables can be stored in different places in memory, depending on their life cycle , variables defined outside the function (global variables or static external variables) and static variables defined inside the function, whose lifetime is the whole process of running the program, This data is stored in the data segment (. data) is a fixed amount of space in memory for these variables, it is divided into two parts for the initialization of the global variables and static global variables, and the other for the initialization of the global variables, because in the case of an operating system, uninitialized static global variables ( static modified variables) are initialized to zero by the compiler, and their lifetime begins at the beginning of the execution of the program to the code block where it resides, until the program leaves the program code block. The invocation of a variable (formal parameter) as a function parameter only exists between the call function period and is stored in the stack space, but it is important to note that the static local variable that is modified by static is only scoped to the block of code but it persists in memory until the next time it is refreshed.

Special attention

Memory space does not actually store variables, but it can be used to store the data that the variable points to, and there is a bit of a pointer meaning, if the result of the malloc () function is assigned to a pointer variable type, then the pointer variable will contain a dynamically allocated memory address, This memory isin a memory space called heap, and the heap is small at first, but when the programmer calls malloc or calloc When the allocation function is equal, it grows, because the heap can have a memory segment with the data segment or stack, and stack overlap occurs when it is greater than a certain limit.


Example code:



Programming language Variables storage age/scope and usage of Static/extern

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.