C + + memory management

Source: Internet
Author: User

I. Memory allocation

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

stack, when executing a function, the storage unit of local variables within the function can be created on the stack, which is freed automatically at the end of the function execution. The stack memory allocation operation is built into the processor's instruction set and is highly efficient, but allocates limited memory capacity. Heap, which is the memory blocks allocated by new, their release compiler does not go to the tube, by our application to control, generally a new will correspond to a delete. If the programmer does not release it, the operating system will automatically recycle after the program finishes. Global/static storage, global variables and static variables are allocated in the same piece of memory, in the previous C language, the global variables are divided into initialized and uninitialized, in C + + There is no such distinction, they occupy the same piece of memory area together.      constant Storage , which is a special piece of storage, they are stored in constant, not allowed to modify.     the free storage area , which is the memory blocks allocated by malloc, is very similar to the heap, but it ends up living with no.

Two. The difference between heap and stack

1. Different Management methods

The stack is automatically managed by the compiler without manual control, while the heap's memory release is controlled by the programmer and is prone to memory leaks

2. Different space sizes

In general, in 32-bit systems, heap memory can reach 4G space, and the size of the stack is generally small, a few m. (You can modify, open the project, and then proceed to the following menu: Project->setting->link, select output in the category, then set the stack's maximum and commit in the reserve.) )

3. Fragmentation issues are different

For heaps, frequent new/delete operations tend to cause a discontinuity in memory space, resulting in a large amount of fragmentation and reduced program efficiency. The stack is not fragmented because of its advanced features.

4. Different direction of growth

The heap is grown in the direction of increased memory address (upward growth);

The stack is grown in the direction of decreasing memory address (growth downward);

5. Different distribution methods

The heap is dynamically allocated;

Stacks are statically allocated or dynamically allocated. Static allocations are done by the compiler, such as the allocation of local variables. Dynamic allocation is performed by the Alloca function, but unlike the dynamic allocation of heaps, its release is performed by the compiler without manual implementation.

6. Different allocation efficiency

Stack allocation efficiency is higher.

The stack is the data structure provided by the machine system, and the computer supports the stack at the bottom: assigning the address of the special register storage stack, and the stack stack has special instructions.

The heap is provided by the C + + function library, the mechanism is complex, for example, in order to allocate a piece of memory, the library function will follow a certain algorithm in the heap memory to search for a sufficient amount of space available, if not, it is possible to call the system Ribbon to increase the memory space within the program data segment.

C + + memory management

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.