What is heap and stack?

来源:互联网 发布:国外听音乐的软件 编辑:程序博客网 时间:2024/05/16 08:32

http://www.maxi-pedia.com/what+is+heap+and+stack

 

The stack is the section of memory that is allocated for automatic variables within functions.Data is stored in stack using the Last In First Out (LIFO) method. This means that storage in the memory is allocated and deallocated at only one end of the memory called the top of the stack. Stack is a section of memory and its associated registers that is used for temporary storage of information in which the most recently stored item is the first to be retrieved.

On the other hand, heap is an area of memory used for dynamic memory allocation. Blocks of memory are allocated and freed in this case in an arbitrary order. The pattern of allocation and size of blocks is not known until run time. Heap is usually being used by a program for many different purposes.

The heap is reserved for the memory allocation needs of the program. It is an area apart from the program code and the stack. the C++ built-in operators new and delete to allocate and deallocate objects in heap memory. the total size of objects allocated on the heap is limited only by your system's available virtual memory.

The stack is much faster than the heap but also smaller and more expensive.