Lifetime of a variable

Source: Internet
Author: User

As we mentioned in the previous lesson, the system allocates a new piece of memory to the function when it executes a function. The local variables and parameter variables of this function are stored in this new memory.

Let's take a look at the distribution status of a C program in the execution time. If you learn the assembly, you should know that when the system is switched on, the memory is divided into two chunks. One is the system area, the operating system and other content, the other is the user area, used to store the executed user program. We mainly study the memory allocation in the user area.

A C program at run time, the user area is divided into three chunks: the first block is the program area, used to store C program running code. The second block is static storage, which is used to store variables, and variables stored in this area are called static variables, such as global variables. The third block is a dynamic storage area, also used to store variables and function calls when the field information and function return address, and so on, the variables stored in this area we call dynamic variables, such as the shape of parametric, the function of the internal definition of local variables.

In the C language, each variable has two properties: the data type and the storage category of the data. The data type is the integer, the real type, and so on that we talked about earlier. The storage category mainly refers to a variable in memory storage area, divided into two major categories: static storage and dynamic storage.

But in the source program of C, how do we specify the storage category of the variable?

I. Static storage variables

All variables defined with the keyword static are all referred to as static variables. All static variables are stored in a static storage area and persist during the program's run.

The static variable defines the position and is divided into global static variables and local static variables. They have some small differences.

Global static variables are actually global variables, and global variables in a program are all stored in the static storage area.

A local static variable refers to a variable that is defined with the static keyword in a function that is scoped only to the function that defines it, but it is stored in a static storage area. We know that a function returns the memory it occupies in return to the system. However, if a static variable is defined in this function, the function will not be released when it returns, and its value is still saved. If we call this function again, we can use this saved value directly.

Look at the following example:

int F ()

{static y=0;

y++;

return (y);

}

Main ()

{int i;

for (i=0;i<5;i++)

printf ("%3d", f ()); }

Operation Result:

1 2 3 4 5_

We define a static variable inside the F function, which is called five times for this function. Although there is an initialization statement inside the function, because y is a static variable, y is initialized only for the first call to the F function, and the other calls use the Y variable directly and no longer initializes.

Description

1, the local static variable if it is not initialized, then the system automatically assigns a value of 0.

2, although the local static variable still exists after the function returns, but because it is a local variable, so other functions can still not reference it.

3. The C language stipulates that only variables stored in static storage can be initialized, which is actually what we mentioned earlier: only global variables and variables defined with static can be initialized.

4, the initialization of static variables is completed in the compilation phase, that is, before the program has been initialized to complete.

Second, dynamic storage variables

Dynamic storage variables are stored in dynamic stores, which are created only when they are defined, and the system reclaims the memory of the variables when the function that defines them returns. The creation and recycling of these variables is done automatically by the system, so it is also called an automatic variable (defined with the keyword Auto). The most typical example is the local variables defined in the function.

In general, the keyword Auto can be omitted, the above line can be written as int b,c=4; They are equivalent. In this way, the shape parametric A is also an automatic variable. In our previous studies, most of what we saw was automatic variables.

In general, all variables are stored in memory, and we know that the computer is a multilevel cache system. When the program is running, only the variables that need to be evaluated are taken from memory to the operator. If there is a variable that is reused over a period of time, such as a loop variable. Then, this process of accessing numbers from within will take a lot of time. So for this reusable variable, the C language allows it to be stored in registers to improve the efficiency of the program. This variable is called a "register variable" and is defined with the keyword register.

Because the number of registers in a computer system is very limited, the number of register variables in the C program is determined to be limited, and only dynamic variables can be used as "register variables". In addition, in some C language systems (such as Turbo C and Ms C), "register variables" are actually handled as automatic variables, and the variables are still stored in memory. So for this kind of variable, we just need to know the line.

Iii. categories of global variables

Here we have learned three types of storage categories static, auto, register. As mentioned earlier, global variables are static variables. However, a C program may be composed of multiple source files. Depending on whether the global variable can be used by other source programs, the global variables are divided into external and internal.

1. If you want to allow other source files to access global variables in this file, define global variables in other source files by using the keyword extern.

This provision of global variables makes it easy to work with multiple people to accomplish a task. Don't worry about the names of your variables.

After studying this section, we know that there are four specific storage categories for variables: (Static,auto.register,extern).

Originally from: http://www.nttvu.edu.cn/sec/kcjx/c/zs/..%5Czs%5Czs79.htm

Lifetime of a variable

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.