[Switch] automatically stored in C ++, dynamic and static memory storage

The method for allocating memory, C ++ has the management data memory 3 by: automatic storage, static and dynamic storage memory (sometimes called free memory space or heap). In the present aspect is the length between the data object assigned to these three different ways. The following briefly describes three types (Note: C ++ 11 added a fourth type - thread storage)
1. Automatic storage
used in a conventional automatic storage space defined within the variable function is called automatic variables (automatic variable), which means they are automatically generated when the function belongs is called, dying at the end of the function. For example, the block defines a temp array () function in a custom getname, when there is only a temp array getname () function activity. When control returns as many main (), temp memory used will automatically be released. If getName () temp return address, the main name pointer () pointed to by the memory it will soon be reused. This is one reason why the use of new in getname () in.
In fact, automatic variable is a local variable, which scope is to include its code block. Code block is a code contained in curly braces.
Automatic variable is typically stored on the stack. This means that the block of code in which the variable is successively added to the stack, while leaving the code block, the release will reverse the order of these variables, it referred to as a LIFO (LIFO). Therefore, during program execution, the stack will continue to grow and shrink.
2. static storage
static storage is storage are present during the entire program execution. Is referred to as static variables in two ways: one is that it is defined outside of the function; Another statis when the keyword is used to declare variables:
static Double Fee = 56.50;
3. dynamic storage
new and delete operators provide a more flexible and static variables than automatic methods. They manage a memory pool, which is called in C ++ free storage space (free store) or heap (heap). The memory pool with memory for static variables and automatic variables are separated. new and delete so you can allocate memory in a function, and release it in another function. Therefore, the statement cycle data is not completely closed life time control program or function. Compared with conventional variables, the use of new and delete allow programmers to use the memory of how the program has greater control. However, memory management more complex. In the stack, automatically adding and removing mechanism makes the amount of memory is always contiguous, and delete new single interaction may lead to a free storage area occupied by the discontinuity, which makes the tracking of newly allocated memory position more difficult.

---------------------
Author: moonlight poet
Source: CNBLOGS
Original: https://www.cnblogs.com/moonlightpoet/p/5647866.html
Copyright: In this paper the author original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/shawnchou/p/11442033.html