Stacks and stacks of operating systems, as those above, not much.
There is also a stack and stack of data structures, which are different concepts. Here, the stack here is actually a data structure (satisfying the nature) priority queue, the first element has the highest priority; the stack is actually a mathematics or data structure that satisfies the nature of advanced.
Although the stack, the stack is connected, but they still have a great difference, even the call is just because of history.
The advantage of the stack is that the allocation of particle size and page boundary can be regarded regardless of it.
The problem, focusing on handling the task of hand. The disadvantage of the stack is that the speed of allocation and release of the memory block is higher than other mechanisms.
Slow, and cannot directly control the submission and recycling of the physical memory.
From the inside, the stack is a region of the reserved address space. At the beginning, most of the pages in the reserved area did not
Submit a physical memory. When more and more memory allocations are made from the stack, the stack manager will put more physical storage
Submit to the stack. Physical memory always allocated from the system's page file, the stack tube when the memory block in the stack is released
These physical memory will be retracted.
Microsoft does not specify the specific rules that the stack release and recovery of the memory in the form of a document.
The rules of Windows 98 and Windows 2000 are different. It can be said that Windows 98 pays more attention to the use of memory,
Therefore, it is only possible, it will return the stack. Windows 2000 pays more attention to speed, so it often takes longer to take up physical deposits
Reservoir, only when the page is no longer used for a while, it will return to the page file. Microsoft often adaptive testing
Try and run a variety of different conditions to determine the most suitable rules in most of the time. With these rules, uses these rules
Dance and hardware changes, these rules will also change. If you understand these rules, you are very critical to your app.
Please do not use the stack. Instead, you can use the virtual memory function (ie Virtualalloc and VirtualFree) so that you can
Enough to control these rules.
I am writing a script interpreter, in order to test the recursive call of the function,
I wrote a script function that caused the function to recurrent 10,000 times. The result appeared
Abnormal, later I will regenerate to 3000 times, it will pass it.
I carefully find the reason, that is, the C program is compiled with GCC, the stack
There is only 2m or so of space.
Everyone can test it with the simple code below:
// Test.c
#include
Void func0 () {
Printf ("Test Func0 / N");
}
Void func1 () {
Char buff [3000000];
Func0 ();
}
Int main () {
Func1 ();
Return 0;
}
// Test.c end
The above code is not wrong when compiling, but at runtime
It will be wrong.
Since the compiler generates some code to implement function calls, these codes are
There is the process of the stack discussed above.
If the 300,00000 in the simple program is changed to 2000000, then
Will n't go wrong.
The stack is pre-stored in the system;
Pile is the free space for your application pointers ~
C dynamic memory is allocated from the heap,
(It is where new and delete operations)
The pile is between the program code and the data memory and the stack, is a free space that can be utilized.
In C and C , there are three basic ways to use stores:
l [Static Memory] In a static memory area, the connector (Linker) allocates space based on the needs of the program. Static variables in global variables, static classes, and functions are allocated in this area. A object allocated in this area is only constructed, and its lifetime is maintained until the end of the program. The address in which the program is running is fixed. In programs that use threads (Thread, shared address space), static objects may cause some problems, because the static objects are shared, and they need to lock operations for their normal access.
l [Automatic Memory]
The parameters and local variables of the function are allocated here. For each of the same function or block, it has its own separate position in this area. This storage is automatically created and destroyed; it is called "automatic storage area". The automatic storage area is also referred to as "Be on the stack".
l [Free Store]
In this area, the program must be clearly applied for the object, and can release the requested space after use after use (using the New and Delete operators). When the program requires more space, use the New to apply to the operating system. Typically, the free storage area (also referred to as a dynamic storage area or heap) is increasing in the survival of a program because there is a space occupied by other programs that never returned to the operating system. . E.g:
INT g = 7; // global variable, allocated in a static storage area
Void f ()
{
INT LOC = 9; // Local variable, allocated in the stack (STACK)
INT * p = new int; // variable is assigned in the free storage area
// ...
Delete P; // Return to the area pointed to in order to reuse
}
For programmers, automatic storage and static storage are always implicit use, which is simple and flashing. The real interesting question is how to manage the free storage area. The allocation space (using the New operator) is very simple, but when it is equipped, there must be a perfect return space for space; otherwise, the storage space will eventually be exhausted (especially the program running for a long time. in).
Comparison of stacks at all levels:
1, from the CPU level
The stacks and stacks have been used directly in directives to the CPU.
(I don't say the data structure characteristics of the stacks and stacks, and there is a very detailed note of any data structure.
The CPU supports the stack of stacks is the stack instructions, PUSH, POP, etc., the heap is actually the memory that has not been organized. It can be said that the entire memory is a big pile. Take a piece from the heap, let it access in the stack access method, which is a stack.
2, from the operating system level
The operating system adds management of virtual memory. We are not in real memory. Take Win32, it is a maximum 4G virtual space, we can do a heap and stack in this virtual space, of course, our operations are virtual, and the actual operation is also handled.
The operating system has effectively managed, independent management. The process is separated from the process, each process has its heap, such as each process has a default heap. In fact, we can even see the memory that is not allocated in the process, you can freely request memory. We know that there is a stack of threads (there is an easy mistake here, some people see the parameters of the function to go to the stack, say that the function has a stack, fault, the stack for the function is the thread it belongs.) We can understand this, the stack is to divide a piece from the pile of virtual big memory, follow the memory of the stack of rules, we can perform PUSH and POP for this memory in the future. 3, from programmer programming level
As long as we know, our programmer is an important thing in memory, and the system provides us with two important and common data structures. In the assembly language, the operation of the stack is PUSH and POP. The PUSH and POP mentioned earlier are concepts in the data structure. Here is the real PUSH and POP on the stack. For the operation of the heap, it is actually a direct operating address. For advanced languages, such as C, for the stack operation channel is hidden, as long as we int A; you can press the variable A to press the stack, and the operation of the heap has become a pair of New and Delete, what we want to use You need to apply first, do not use it directly to assembly language. Application is because the operating system knows that the operating system is managed.