C language understanding of different types of variables from a memory perspective

Source: Internet
Author: User
Tags joins

We all know that in C language, different types of variables are stored in memory in different places, from the memory point of view, the different memory blocks of the code or the variables whose properties (mainly scope, life cycle, link properties) are different.

First, let's introduce the concept of scope, life cycle already linked properties


1: Scope

Scope refers to the extent to which the variable is acting.

(1) A local variable is a code block scope (that is, the scope of the local variable is {})

(2) Functions and global variables are file scopes, and further their scope is the definition of functions and global variables or the parts of declarations.


2: Life cycle

The life cycle is the time of generation and extinction of variables.

(1) The life cycle of the stack variable: The local variable is assigned to the stack (stack variable), its life cycle is temporary, the temporary meaning is to create, use, and perish as needed. For example, a local variable int A is defined in a sub-function, so when the child function is called to execute, the local variable int A; is created on top of the stack (the symbol A is bound to a memory above the stack), and when the child function is called, the local variable int A is extinct (canceling the binding of a memory on the symbol A and the stack), the real example is that when we write a program to read the address of a local variable, the address value of each read is different.

(2) Life cycle of heap variables

First of all, it is necessary to understand that the heap memory space is objective, it is a memory space maintained by the system, the programmer simply applies to the system heap memory, and then release, so the heap variable is also a life cycle, its life cycle is mallloc (return to the application heap memory of the first address) to apply to free release,

(3) Life cycle of data segment and BSS segment

The lifetime of a global variable is permanent, and permanent means starting with the definition and ending at the end of the program.

(4) Life cycle of code snippets, read-only segments

The code snippet holds the code of the program execution (that is, the function), its life cycle is permanent, but generally do not study the life cycle of the code, it should be noted that the code snippet is also a const-modified constant and character constants (const type of constants, string constants are sometimes placed in the Rodata segment, Sometimes put in code snippets, depending on the platform)


3: Link Properties

Link properties are the attributes of the code at the time of the link, including outer joins, internal links, no links, three types of

(1) Outer connection: means an external link property, which means that it can be linked throughout the program scope (which can span files)

(2) Inner link: meaning internal link property, that is, can be linked within the current C file scope

(3) No link: meaning that the symbol itself does not participate in the link, all local variables (auto, static) are non-linked


Note that there is also a problem with a variable mask rule with the same name

(1) Problem: When programming, the variable with the same name will inevitably appear. A variable with the same name does not necessarily make an error.

(2) First, if two variables of the same name have different scopes and do not overlap, in this case the same name has no effect. For example, two sub-functions are defined in the local variable int A;

(3) Secondly, if two variables with the same name have overlapping scopes, C language is defined in the scope of overlap,

A variable with a small scope will cloak the one that is larger in scope (in the small scope principle). For example, a global variable int A is defined, and a local variable int A is defined in the sub-function, then the local variable int A is the function of the sub-functions;



Next, from the variable type angle, the analysis code this three dozen elements concrete to the different variable significance

1: Normal local variables

Scope is the scope of the code block, the life cycle is a temporary life cycle, link properties are not linked

Ordinary local variables are assigned to the stack, created, used, and extinct on demand, and each time the stack is assigned to a variable, the address is indeterminate (randomly assigned), which determines that its link attribute is no link.


2: Static local variable (static modified local variable)

a static local variable is assigned to a data segment or a BSS segment (the display is initialized to not 0 is assigned to a data segment, not explicitly initialized or explicitly initialized to 0 is assigned to the BSS segment), scoped to the code block scope, and the lifetime is a permanent life cycle, The Link property is a no link (because its scope is a code block scope). because a static local variable is assigned to a data segment or a BSS segment, its life cycle is permanent, the binding of the assigned address and the static local variable's symbol is constant until the end of the program, so the same static local variable is used more than once in a program, and the same memory is actually manipulated. Because of this feature, static local variables are often used to count the number of times a child function is called.


3: Global variables and normal functions

Scope is file scope, life cycle is persistent, link properties are outer joins

It is important to note that global variables and functions are not defined in the header file because a program calls the header file in multiple places (the contents of the header file are duplicated in the file when it is preprocessed ) (the function that uses inline modifiers, of course, can be placed in a file , and will not be defined repeatedly in urban and rural areas). The outer join property of a global variable determines that it must be a file scope and a permanent life cycle.











This article is from the "11664570" blog, please be sure to keep this source http://11674570.blog.51cto.com/11664570/1870075

C language understanding of different types of variables from a memory perspective

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.