The conquest of the C pointer extracts the scope and life cycle of the 2:C variable (storage period)

Source: Internet
Author: User

When developing small programs, perhaps we don't care about the need for scopes. However, when you write tens of thousands of lines, or even hundreds of thousands of lines of code, no scope is definitely intolerable.

The C language has the following 3 scopes.

1. Global variables

A variable declared outside of a function is, by default, a global variable. Global variables are visible everywhere. When a program is split into multiple source code files for compilation, variables declared as global variables can also be referenced from other source code files.

2, static variables inside the file

Even for variables that are defined outside the function like global variables, once staticis added, the scope is limited to the current source code file. Variables specified by static (including functions) are not visible to other source code files. In English, static is the meaning of the word, I really do not understand why this function is somehow dubbed "static", this is a C language is an unsolved mystery.

3. Local Variables

A local variable refers to a variable declared in a function. A local variable can only be referenced in the statement block that contains its declaration (the scope enclosed in {} ).

Local variables are usually declared at the beginning of a function, but can also be declared at the beginning of a statement block inside a function. For example, it is convenient to place a local variable declaration at the beginning of the current statement block when you need to use a temporary variable when swapping the contents of 2 variables.

A local variable is usually released at the end of the block of statements where it resides. If you do not want to release a local variable, you can declare it by adding static to the local variable.

In addition, there is a difference between the variables of C and the storage period (storage duration) In addition to the scope.

1 static storage period (static storage duration)

A global variable, a static variable within a file, and a local variable that specifies static hold a static storage period . These variables are collectively referred to as static variables .

The lifetime of a variable holding a static storage period starts when the program runs and ends when the program closes. In other words, static variables always lie in the same address of memory.

2. Automatic storage period (Atuo storage duration)

There is no local variable that specifies static, and the automatic storage period is held. Such variables are referred to as automatic variables .

The variable that holds the automatic storage period is allocated to the memory area when the program runs into the block of statements it is in, and the memory area is freed after the statement block execution ends.

This feature is typically implemented using the "stack" mechanism.

3, Next is not a "variable", C can use the malloc () function to dynamically allocate memory. The memory is dynamically allocated through malloc (), and the lifespan continues until it is released using free ().

In a program, if you need to keep some data, you must obtain a corresponding size of memory area in a place in memory. To summarize, there are three memory area lifetimes in C.

(1), static variable

The lifespan begins when the program runs and ends when the program shuts down.

(2), automatic variable

The lifetime of the statement block that declares the variable is executed until the end of execution.

(3) Areas allocated through malloc ()

Lifetime to call Free ().

The conquest of the C pointer extracts the scope and life cycle of the 2:C variable (storage period)

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.