Stack is post-in, first-out, and heap is random.
---------------------
It is a data structure used to manage buckets,
Stack is used to allocate space for local variables, with a small capacity
Heap is used to manage large data blocks. The capacity is related to system resources.
----------------------
Stack is an ordered (FILO) structure, and stack is an unordered structure.
The heap and stack in the program running are different from those above. Does Assembly know? For example, the following program
Int max (int A, int B)
{Int x = 0 ;//
If (A> B)
Return;
Else
Return B;
}
When a function is called, the data is imported into the stack. The called function retrieves data from the stack to complete calculation. The stack structure is mainly used to call data for easy cleanup.
Heap, as the old man said above, is the space for variable data usage. For example, if the buffer of a read file does not know how much space to allocate at first, the fixed structure of the stack cannot be satisfied, so we have to use the heap structure.
------------------------------
The program has at least three data segments: Global Data zone, stack, and stack.
Global is the data that stores the global variables in the C language. These variables are automatically released at the end of the program.
Heap segments are used to store dynamically allocated variables, such as new variables in C ++. These variables must be manually released.
Stack segments are generally used to store variables in functions.
(Variables dynamically allocated in the function are allocated in the heap if the new variable is used)
This type of variable is automatically released when it is out of the stack (such as function return)
-------------------------------
1. The heap is a free storage area much larger than the stack. The memory space allocated on the heap cannot be automatically released and must be manually released.
The stack is also a storage area in the memory. The space allocated on the stack can be automatically released as the variable or object scope ends.
For example, it is easier to understand
Cbutton m_button; // This way of defining variables is the memory space allocated to the m_button object on the stack. With the completion of the m_button object scope, the memory space occupied by this object can be automatically released.
Cbutton * pbutton = new cbutton ();
In this way, the memory space allocated to the cbutton object directed by pbutton is allocated on the heap. Even if the scope of the object ends, the bucket will not be automatically released, you must manually use Delete pbutton to release
2. If you want to control the life cycle of a variable, allocate a storage area for the variable on the stack, for example, in a multi-threaded Program
Cbutton m_button;
Afxbeginthread (func, & m_button); // pass the button pointer as a parameter to the thread function
// Thread Functions
Uint func (lpvoid lparam)
{
Cbutton * pbutton = (cbutton *) lparam;
Pbutton-> // errors may occur here, because when the thread runs here, the program block where m_button is located may have ended, that is, the role of m_button.
The domain has ended. Because m_button is the storage space allocated on the stack, its scope has ended, meaning that its storage space has been released, here, pbutton is also used to access the released
Memory, the error of illegal memory access will occur.
Return 0;
}
To solve the problem above, you can allocate space for the cbutton object on the heap and manually release the object when it is not needed.
If the above Code can be changed
Cbutton * pbutton = new cbutton (); // allocate space on the heap
Afxbeginthread (func, pbutton );
Uint func (lpvoid lparam)
{
Cbutton * P = (cbutton *) lparam;
P-> // in this way, no error will occur after the access. After using the pointer, you can call Delete P; to release the heap space.
Return 0;
}
--------------------------------------------
Stack is a kind of memory resource reserved for the process by operating the creation process. It is mainly used for the allocation of local variables. Compared with the heap, stack allocation is much faster. The user process uses malloc/free
(New/delete) the space requested from the system must be manually released, which is relatively slow. For example, Java fully uses the heap to manage the memory, and C/C ++ and C # can all be obtained from the use of the stack.
Benefits!
--------------------------------------------
Summary from http://topic.csdn.net/t/20040409/14/2945507.html #