Memory Distribution in C ++

Source: Internet
Author: User

In C ++, memory is divided into five areas: heap, stack, free storage, global/static storage, and constant storage.
STACK: it is a variable storage area automatically allocated by the compiler when needed and cleared when not needed. It usually stores local variables and function parameters.
Heap: memory block allocated by newProgramMember release (the compiler does not care). Generally, a new corresponds to a delete, and a new [] corresponds to a Delete. If the programmer does not release the resource, the resource will be automatically reclaimed by the operating system after the program ends.
Free storage zone: it is a memory block allocated by malloc and so on. It is very similar to the heap and is released with free.
Global/static storage zone: global variables and static variables are allocated to the same memory. (in c, global variables are classified into initialized and uninitialized, c ++ does not have this distinction ).
Constant storage area: this is a special storage area where constants are stored and cannot be modified.
(Note: The heap and free storage areas are actually the same region, and the new underlying implementationCodeMalloc is called, and new can be regarded as an advanced version of malloc intelligence)

Stack and stack discussion:
Management Method:
Resources in the heap are controlled by programmers (memory leak is easily generated ).
Stack resources are automatically managed by the compiler without manual control.

System response:
For heap, you should know that the system has a linked list that records the idle memory address. When the system receives a program application, it traverses the linked list to find the first heap node with a space greater than the requested space, delete the node in the idle node linked list and allocate the Node space to the Program (most systems will record the size of the allocation at the first address of the memory space, in this way, delete can correctly release the memory space, and the system will re-place the excess parts into the idle linked list ).
For a stack, the system provides the program with memory as long as the remaining space of the stack exceeds the applied space. Otherwise, an exception is reported, prompting stack overflow.
Space size:
Heap is a non-consecutive memory area (because the system uses a linked list to store idle memory addresses, it is naturally not consecutive ), the heap size is limited by the effective virtual memory in computer systems (32 bit systems are 4 GB in theory). Therefore, the heap space is flexible and large.
Stack is a continuous memory area with a predetermined size of the operating system. The stack size in Windows is 2 m (OR 1 m, which can be set in VC during compilation ).
Fragmentation problem:
For heaps, frequent New/delete operations can cause a large number of fragments, reducing program efficiency.
For stacks, it is an advanced and post-output queue, with one-to-one access and no fragmentation.
Growth direction:
Heap up, to the high address direction growth.
Stack down, to the low address direction growth.
Allocation Method:
Heap is dynamically allocated (no static heap allocation ).
The stack has static allocation and dynamic allocation. The static allocation is completed by the compiler (such as local variable allocation), and the dynamic allocation is allocated by the alloca function, but the resources dynamically allocated by the stack are released by the compiler, no need for programmers.

Allocation Efficiency:
The heap is provided by the C/C ++ function library, and the mechanism is complex. Therefore, the heap efficiency is much lower than the stack efficiency.
Stack is a data structure provided by an extremely systematic system. Computers provide support for stacks at the underlying layer, allocate special registers to store stack addresses, and stack operations have special commands.

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.