memory allocation in C + + program runtime "Turn"

Source: Internet
Author: User

In C + +, memory is divided into 5 extents: heap, stack, free storage, global/static storage, and constant storage.

1, Stack: The compiler is automatically allocated when needed, and does not need to automatically clear the variable storage area. Local variables, function parameters, etc. are usually stored.

2, Heap: Is the memory block allocated by new, released by the programmer (compiler regardless of), generally a new with a delete corresponding to a new[] and a delete[] corresponding. If the programmer is not released, the resource will be automatically reclaimed by the operating system after the program ends. Free Storage: is a block of memory allocated by malloc, such as a heap, which is very similar to that used for free. (Note: The heap and free storage are actually just the same area, the new underlying implementation code calls the Malloc,new can be seen as the advanced version of malloc Intelligence)

3, Global/static storage: Global variables and static variables are allocated in the same piece of memory (in C, the global variables are divided into initialized and uninitialized, C + + does not have this distinction).

4, Constant storage: This is a special storage area, inside a constant, not allowed to modify.

Heap vs. Stack discussion:

Management method:

The resources in the heap are controlled by the programmer (easy to generate memory leak).

Stack resources are automatically managed by the compiler without manual control.

System response:

For the heap, you should know that the system has a list of idle memory address, when the system receives a program request, traverse the list, look for the first space is larger than the application space of the heap node, delete the idle node linked list of the node, The node space is allocated to the program (most systems record the size of this allocation at the first address of the memory space, so that delete can properly free up the memory space, and the system will re-place the extra parts into the idle list).

For the stack, as long as the remaining space on the stack is larger than the requested space, the system provides memory for the program, otherwise the exception prompt stack overflow.

Space size:

Heap is a discontinuous area of memory (because the system uses a linked list to store free memory addresses, nature is not continuous), the heap size is limited by the effective virtual memory in the computer system (32bit system theoretically 4G), so the heap space is more flexible, larger.

Stack is a contiguous area of memory, the size of the operating system is scheduled, Windows stack size is 2M (there is 1M, at compile time, VC can be set).

Fragmentation issues:

For heaps, frequent new/delete can cause a lot of fragmentation, which reduces program efficiency.

For stacks, it is an advanced post-out queue that corresponds to and from one by one and does not produce fragmentation.

Growth direction: (This determines the stack in the high address area, heap in the bottom address area)

Heap upward, increasing in the direction of the high address.

Stack down, increasing in the low address direction.

Allocation method:

Heaps are dynamically allocated (there is no statically allocated heap).

Stacks have static allocations and dynamic allocations, static allocations are done by compilers (such as local variable allocations), and dynamic allocations are allocated by the ALLOCA function, but the dynamically allocated resources of the stack are freed by the compiler without the programmer's implementation.

Allocation efficiency:

The heap is provided by the C + + function library and the mechanism is complex. So the heap is much less efficient than the stack.

Stack is a very system-provided data structure, the computer in the bottom of the stack to provide support, the allocation of special register storage stack address, stack operation has a special instruction.

memory allocation in C + + program runtime "Turn"

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.