6. lifetime of variables

Definition: The lifetime of a variable is the time period that take up memory space in the program is running.

1. Static survival

    Withdraw from the assignment when the program started by the end of the program.

    Such as: global variables

2. Automatic survival

    When the statement is finished composite recover from allocation to define their program execution when the compound statement.

    Such as: local variables , function parameters

3. Dynamic survival

    With a new operation or malloc function to allocate, use the delete operator or free function to call back.

Note:

1. In the definition of local variables , the use of auto, static, register explicitly indicate their lifetime.

    auto: default, automatic survival   

    static: Static lifetime, when the function is called multiple times, you can hold the results of the last invocation .

    register: automatic lifetime, and auto except that recommended (but not necessarily) compiler spatial distribution of the local variables in the CPU registers, its purpose is to improve the efficiency of access to local variables.

2. The memory space is divided into four parts: static data area , the code region , the stack area , the heap area

    Static data area : global variables , local variables static memory classes , constants

    Area Code : Code Function

    Stack area : the local variable auto storage class , a function parameter , information about the function call (e.g., return value )

   Heap : dynamic variables

    The size of the static data area and the code area is fixed, and run the program with the size of the stack area and heap area changing (though the operating system to its maximum space have certain restrictions).

3. The compiler will automatically implicitly static lifetime of a variable bit mode is initialized to 0 , other variables not be initialized (using the space has value, and therefore is not fixed)

 

Guess you like

Origin www.cnblogs.com/tuzi140301/p/11415186.html