Automatic storage, static storage, and dynamic storage in C + +

Source: Internet
Author: User

Based on the method used to allocate memory, there are 3 ways to manage data memory in C + +: Automatic storage, static storage, and dynamic storage (sometimes called free storage space or heap). In terms of the length of existence, the data objects allocated in these three different ways are different. The following is a brief introduction to these three types (note: The fourth type is new in c++11-thread store)
1. Auto-store
The general variables defined inside the function use automatic storage space, called an automatic variable (automatic variable), This means that they are automatically generated when the function they belong to is called and die at the end of the function. For example, when a temp array is defined in a custom function getname (), the temp array exists only when the GetName () function is active. When you return to main () as a permission control, the memory used by temp is automatically freed. If GetName () returns the address of temp, the memory pointed to by the name Pointer in main () will soon be reused. This is one of the reasons for using new in GetName ().
in effect, an automatic variable is a local variable whose scope is the block of code that contains it. A code block is a piece of code that is enclosed in curly braces. The
automatic variables are usually stored in the stack. This means that when the code block is executed, the variables are added to the stack in turn, and the variables are released in reverse order when they leave the code block, which is known as last-in-first-out (LIFO). As a result, the stack will continue to grow and shrink during program execution.
2. Static storage
Static storage is a storage method that exists throughout the execution of the program. There are two ways that variables are called static: one is to define it outside the function, and the other is to use the keyword statis when declaring a variable:
static double fee = 56.50;
3. Dynamic storage The
new and delete operators provide a more flexible approach than automatic variables and static variables. They manage a pool of memory, which in C + + is called free storage or heap. The memory pool is separate from the memory used for static variables and automatic variables. New and delete allow you to allocate memory in one function and release it in another function. Therefore, the declaration period of the data is not completely controlled by the life time of the program or function. Using new and delete gives programmers greater control over how the program uses memory than using regular variables. However, memory management is also more complex. In the stack, the automatic add and remove mechanism makes the memory occupied always contiguous, and the interplay of single new and delete may result in a non-contiguous free storage area, which makes it more difficult to track the location of the newly allocated memory.

Automatic storage, static storage, and dynamic storage in C + +

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.