The difference between stack memory and heap memory in C + +

Source: Internet
Author: User

    • Heap and stack in data structures:

Stack: is a continuous storage of data structure, with advanced post-out nature. The usual operations are in the stack (tumbler stack), the stack, and the top element of the stack. To read an element in the stack, it is necessary to stack all of its previous elements before it can be completed. Analogy with the real box.

Heap: is a discontinuous tree storage data structure, each node has a value, the whole tree is sorted . The feature is the minimum (or maximum) value of the root node, and the two subtrees of the root node are also a heap. Used to implement the priority queue, access arbitrary.

    • In-memory stack and heap areas:

Generally speaking of memory, refers to the computer's random storage (RAM), the program is running in this area. The approximate breakdown of computer memory is as follows:

Stack Memory : The program automatically applies to the operating system to allocate and recycle, fast, easy to use, but the programmer can not control. If the allocation fails, the stack overflow error is indicated. Note that the const local variable is also stored in the stack area, and the stack area increases in the direction of the address decrease.

Test stack Memory # # <iostream>int main () {int i = 10;//variable i stored in the stack const int i2 = 20;int i3 = 30;std::cout << &i << "<< &i2 <<" "<< &i3 << std::endl;return 0;}

The test output is:

&i3 < &i2 < &i, proving the address is reduced.

Heap Memory : The programmer applies a piece of memory to the operating system, and when the system receives the application, it iterates through a linked list that records the free memory address, finds the heap node where the first space is larger than the requested space, and then removes the node from the list of idle nodes and assigns the node's space to the program. The allocation is slow, the address is not contiguous, easy to fragment. In addition, the programmer must also be responsible for the destruction of the programmer, otherwise it will lead to memory leaks .

The differences between test heap memory and stack memory include <iostream>int main () {int i = 10;//variable i stored in the stack, char pc[] = "hello!";//stored in the Stack area const DOUBLE CD = 99 .2; stored in the stack area static long si = 99; Si is stored in a read-write area, dedicated to storing global variables and static variables in memory int* pi = new int (100); The memory that the pointer Pi points to is in the heap area, the memory allocated when the program is dedicated to storage std::cout << &i << "<< &pc <<" << &cd <& Lt "<< &si <<" "<< pi << std::endl;delete pi; Requires the programmer to release return 0;}

The test output is:

Running multiple times will find that the Pi point to the address is not continuous, is jumping, and the &si is consistent, stored in the read-write area, the first three variables are stored in the stack area, automatically allocated and destroyed by the program.

    • A metaphor for heap and stack differences:

The difference between heaps and stacks can be seen by referring to the analogy of a predecessor:

Use the stack like we go to a restaurant to eat, just order (send application), pay, and eat (use), eat enough to go, do not bother to cut vegetables, wash vegetables and other preparation work and washing dishes, brush pots and other finishing work, his advantage is fast, but the freedom is small.

The use of the heap is like a DIY dish that you like to eat, more trouble, but more in line with their own tastes, and great freedom.

The difference between stack memory and heap memory 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.