All kinds of variable life cycle and storage space

Source: Internet
Author: User

I. Introduction to the run-time environment

During execution, the program runs within its own logical address space, where each program value has an address within that space. A typical program space pattern such as:

First, the data at run time contains the data area and code area. The text in the diagram is the code area that stores the target code. The data area includes the figures, BSS, Heap, and stack in the diagram.

1. ( constant area ) The data area mainly stores constants, mainly constants that require more space, such as char s[32]= "Hello World";

2. ( Global zone ) BSS primarily stores global and static variables.

3. Heap is a dynamic storage space, which is used to dynamically request storage space during the process of running.

4. ( stack ) stack provides storage space for functions at run time, and local variables are in this space. Of course, in addition to local variables, the stack also holds a variety of data necessary to implement function calls.

To make more efficient use of storage space, the heap and stack are placed in opposite directions, with each region growing in the same way as the other. When implemented, the stack grows toward the lower address direction, while the heap grows toward the higher address direction.

Second, local variables and global variables

Variables defined inside the function are allocated on the stack, and when the function is called, the variable is allocated, and when the function returns, the variable is freed, so the local variable is only valid within the function, and is only used inside the function, so it is called a " local variable ", also known as an "internal variable."

The lifetime of a local variable is a call cycle of a function.

Description

Different functions can use the same name of the local variables, do not occur between each other, the main function is very special, but in the use of local variables and other functions are not different. Finally, the formal parameters used to receive or store parameters are also considered local variables and can be used in the same way as local variables.

Variables defined outside the function are allocated in the BSS, and are not valid for function call procedures, so other functions can access such variables, called global variables .

1) Scope of global variables: Global variables can be accessed by other functions in this file, whose valid range is from the location where the variable is defined to the end of the file. This variable can be accessed by all functions within this valid range, allowing the global variable to be used as a channel for exchanging information between functions.

2) The lifetime of global variables: Global variables always occupy the storage unit during program execution, that is, the lifetime of the global variable is the lifetime of the program. Therefore, if you use a large number of global variables, you may make the program occupy too many storage units, thereby reducing the available storage space for other programs.

Three, dynamic storage and static storage

Local variables and global variables are mainly classified from the storage space angle, and can be divided into static storage and dynamic storage from the life cycle of variables.

Static storage means that the spatial allocation of variables is not determined by the program health, but by the way the program is fixed during the run.

In dynamic storage mode, the allocation of variables will be based on program run conditions (such as function calls, dynamic memory allocations, etc.). Allocates variable space when a function call occurs, freeing the variable space at the end of a function call.

Here's an example to understand the variable lifetime.

Example 1: (global variable and local variable lifetime)

int   global_a;  int   Local_1 () { char      Local_1_c; Local_2 ();}  int   local_2 () { char   Local_2_c;}  int   main () { int      Main_i;  for  (Main_i=0 ; main_i<3 ; Main_i++ return  0  ;}  

1) Assuming that the T=1 program starts, the global variable global_a begins to exist until the program ends; The main_i in the main function also begins to exist until the main function exits.

2) t=2, when the main function calls the Local_1 function, the Local_1_c variable is allocated space, forming 3 variables to survive simultaneously.

3) t=3, the Local_1 function calls the Local_2 function, because the Local_2_c variable is also assigned to form 4 variables that coexist in the program.

4) When the Local_2 function is t=4, the Local_2_c variable will be released, resulting in the coexistence of Local_1_c, Main_i, and Global_a in T=5.

5) t=5, the Local_1 function ends, so the Local_1_c variable is freed. Only Main_i and global_a coexist when the t=5 is caused.

The situation is similar to the previous.

As can be seen from the above example, the C program starts from the spatial scope, takes the default static allocation policy for global variables, and takes the default dynamic allocation policy for local variables . The C program also provides a variable allocation method that explicitly indicates the storage class. In a C program, you can specify the storage category by using the keyword auto and static . Variables in the Auto category will dynamically allocate storage space, and data is stored in dynamic storage, such as the function's formal parameters and the default allocation of local variables. Variables of the static category are stored in the static data area and survive during program execution.

voidauto_static ();intMain () {inti;  for(i=0;i<5; i++) {auto_static (); } printf ("\ n"); Exit (0);}voidauto_static () {intvar_auto=0; Static intVar_static=0; printf ("Var_auto is%d--", var_auto++); printf ("Var_static is%d\n", var_static++);}

Program Run Result:

In the function auto_static, the description statement int var_auto=0 is defined, indicating that Var_auto is a local variable, and because there is no storage class specified, dynamic storage is used by default.

Define Description Statement

static int var_static=0;

Indicates that var_static is a local variable and specifies that the storage class is static, so it will be stored statically, and var_static will allocate memory space after the program starts without waiting for the auto_static () function call to occur.

Iv. internal functions and external functions

When a program consists of multiple source files, you can specify that a function within a file can be called by another file, or you can specify that the function can only be used by this file. In this sense, functions can be divided into external functions and intrinsic functions, and by specifying function class qualifiers to restrict the invocation of functions.

Functions are inherently external, and function names follow the same scope rules as the variable names. C language does not allow the definition of function nesting, each function is a parallel side-by-side relationship, can be called between each other, thus the function has a global effective range. But each function definition is attached to a program file, similar to a variable. We can decide whether the function's callable scope is all program files or only the file where the function definition is located, that is, whether the function is an external function or an intrinsic function.

1. External functions

When a function is defined, the keyword extern indicates that the function is an external function, for example:

extern double func1 (double x) {...}

The function func1 can be called scope (scope) is all program files, that is, it can be called by any other function, whether or not this function is in the same program file as the function func1 (). The C language stipulates that if you omit the extern keyword when you define it, the default is an external function.

2. Intrinsic functions

When a function is defined, if the keyword is static, it means that the function is an intrinsic function, for example:

Static double func1 (double x) {...}

Such a function func1 can be called scoped (scoped) only to the file where the function definition is located, that is, the function is restricted to be called only by functions in the program file. Internal functions, also known as static functions, when using intrinsic functions, the invocation of rain function is confined to the file, so in different files can define the same name of the internal functions and do not interfere with each other, so as to facilitate the writing of large applications.

All kinds of variable life cycle and storage space

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.