Reproduced Simple Introduction to C + + heap and stack

Source: Internet
Author: User

In C and C + +, there are three basic ways to use the storage area:

[Static Memory]

In the static store, the connector (linker) Allocates space for the object according to the requirements of the program. Global variables, static class members, and static variables in the function are assigned to the zone. An object allocated in the region is constructed only once, and its lifetime is maintained until the end of the program. The address is fixed when the program is running. In programs that use threads (thread, concurrency in shared address space), static objects can cause problems because static objects are shared, and locking is required for normal access.

[Automatic storage area (Automatic memory)]

The parameters and local variables of the function are assigned here. For each invocation of the same function or chunk, it has its own position within the area. This type of storage is automatically created and destroyed, and is therefore called an "automatic storage area". Automatic storage is also known as "on the Stack" (being on the stack).

[Free Storage Area]

In this area, the program must explicitly request space for the object, and can release the requested space (using the new and delete operators) after the use is complete. When the program needs more space, use new to apply to the operating system. In general, free storage (also known as dynamic storage or heap) is increasing over the lifetime of a program because the space occupied by other programs is never returned to the operating system. For example:

int g = 7; Global variables, allocated in static storage
void F ()
{
int loc = 9; Local variables, allocated in stacks (stack)
int* p = new int; Variables are allocated in the free storage area
...
Delete p; Return the area to which P is pointing so that it can be reused
}

For programmers, automatic storage and static storage are always used implicitly, and this is both simple and straightforward. The really interesting question is how to manage the free storage area. Allocating space (using the new operator) is simple, but when it comes to matching, there must be a complete scheme for returning space, otherwise the storage space will eventually be exhausted (especially in long-running programs).
The simplest solution to this problem is to use automatic objects that correspond to objects in the free store to handle allocation and de-provisioning. Based on this, many container are implemented as the master of the objects in the free store (handle).

Reproduced Simple Introduction to C + + heap and stack

Related Article

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.