First of all, the statement here is the C language heap and stack, not in the data structure !
First, Preface introduction:
C Language program compiled after the formation of a compiled, connected after the formation of the binary image file by the stack, heap, data segment (composed of three parts: read-only data
Segment, the read-write data segment has been initialized, the uninitialized data segment is the BBS), and the code snippet is composed, as shown in:
The so-called static , is bound to exist and will exist forever, will not disappear, such data including constants, constant variables (const variable), static change
variable, global variables, and much more.
Dynamic, it's going to change. The dynamic area is the heap and stack. This stack should be called the call stack, which will hold the return address of the function,
Parameters and local variables. Stacking is the space we allocate with the new operator and the malloc function.
Specific to:
1.Stack Area(stack): The compiler automatically assigns the release,the parameter value of the stored function, the local variable equivalent. Local variables, task thread functionsAnd the like is put in
(using) The stack, the stack utilization is higher. It operates in a manner similar to a stack in a data structure. Specialstacks are thread-owned, each thread will have a
A stack of your own
2.Heap Area(heap): Typically released by programmers, which can cause a memory leak if the programmer does not release it. Note that it is not the same as the heap in the data structure
, the distribution method is similar to the list, the common ismalloc came out of the heap area ., like a fixed area, is released when it's free,
Pointsimilar to the global, static。
3. Program code area: the binary code that holds the function body.
4. Data segment: Consists of three parts:
1>Read-onlyData segment:
Read-only data segments are data that the program uses that are not changed, and use this data in a way that resembles a table-type operation, since these variables do not need to be changed.
So you just need to place it in read-only memory. is generallyConst-ModifiedVariables and literal constants used in programs are typically stored in read-only data segments
。
2> initialized read-write data segment:
Initialized data is a variable that is declared in the program and has an initial value, which takes up space in memory and is required to be located in the program when it is executed.
Read-write memory area, and has an initial value, for the program to read and write when running. In the program is generallyA global variable that has been initialized, a static office that has been initialized
part variable (the static modified variable that has already been initialized)
3> uninitialized Segment (BSS):
Uninitialized data is declared in the program, but there are no initialized variables that do not need to occupy memory space before the program runs. and read and write numbers
According to paragraph, it also belongs to the static data area. However, the data in this paragraph is not initialized. Uninitialized data segments are generated only during the initialization phase of the run
, so its size does not affect the size of the target file.in a program, there are generally no initialized global variables and static local variables that are not initialized。
Second, the difference between the two:
1. How to Apply
(1) stack (SATCK): By the systemAuto Assign。 For example, declare a local variable int b in the function, and the system automatically opens up space for B in the stack.
(2) Heap: Requiredprogrammers apply themselves(call Malloc,realloc,calloc), indicating the size, and released by the programmer. Easy to produce
Memory leak.
Eg:char p;
p = (char *) malloc (sizeof (char));
However, p itself is in the stack.
Using the new operator in C + +
such as P2 = new CHAR[10];
But note that P1, p2 itself is in the stack.
2. Restrictions on size of applications
Allocating locations in memory is related to both the hardware architecture and the operating system, where the stack is allocated from a high address to a low address, and the heap is x86 from a low address to a high address.
Stack and stacks in Windows and Linux
(1) Stack: The stack in Windows is the data structure that extends to the bottom address, which is a contiguous area of memory (it grows in the opposite direction as the memory is growing).
The size of the stack is fixed. If the requested space exceeds the remaining space on the stack, overflow will be prompted. The meaning of this sentence isThe address of the top of the stack and the maximum capacity of the stack
The amount is pre-defined in the system., in Windows, the size of the stack is 2M (also some say 1M, in short, is a compile-time determined constant), if the application
When the space exceeds the remaining space on the stack, the overflow will be prompted. Therefore, the space available from the stack is small.
(2) Heap: A heap is a data structure with a high address extension (it grows in the same direction as the memory) and is a discontinuous area of memory. This is due to the system
The use of a linked list to store free memory addresses is naturally discontinuous, and the traversal direction of the list is from the bottom address to the high address.the size of the heap is limited by the computer system
valid virtual memory in the system。 Thus, the space of the heap is more flexible andrelatively large。
3. System response:
(1) Stack: As long as the stack of space is larger than the application space, the system will provide memory for the program, otherwise it will report the exception prompt stack overflow.
(2) Heap: First you should know that the operating system has a record of the free memory addressLinked list, but when the system receives the application, it iterates through the list, looking for
A heap node that is larger than the requested space, then removes the node from the idle list and assigns the node's space to the program, in addition, for most
Number system, the size of this allocation is recorded at the first address in this memory space, so that the free statement in the code can properly release the memory space.
In addition, the size of the found heap node is not necessarily exactly equal to the size of the request, and the system automatically re-places the extra portion into the idle list.
Description: For heaps, forHeap, frequent new/delete is bound to cause memory space discontinuity, resulting in a large number of fragments, so that program efficiency
lower. For the stack, there is no problem.
4. Application Efficiency
(1) Stack by The system is automatically assigned, and the speed is fast. But programmers can't control it .
(2) The heap is made up ofThe memory allocated by malloc is generally slow and prone to fragmentation, but it is most convenient to use .。
In addition, under Windows, the best way is to allocate memory with VirtualAlloc, he is not in the heap, nor is the stack directly in the address space of the process to protect
Leave a piece of memory, although it is most inconvenient to use. But the speed is fast, also the most flexible.
5. Storage content in heaps and stacks
(1) Stack: When a function is called, the first stack of the primaryfunctionThe address of the next statement, followed by the parameters of the function, which is entered from right to left
Stack, and then a local variable in the function. Note: Static variables are not in the stack.
When the function call ends,Local VariablesFirst out of the stack, then theParameters, the last stack-top pointer points to the first saved address, which is the next bar in the main function
command, the program continues execution from that point.
(2) Heap: The size of the heap is usually stored in a heap with a byte in the head. The concrete content in the heap is determined byProgrammer's Schedule。
6. Access efficiency
(1) Heap: char *s1= "Hellow Tigerjibo";compilation is determined.
(2) Stack: char s1[]= "Hellow Tigerjibo";run-time assignment value;
However, in subsequent accesses, thearray on stack (variable, partial)than what the pointer points tostring (for example, heap, immutable, static)Quick, the pointer in the bottom assembly needs to use the EDX register to relay a
And the array is read on the stack.
#include
void Main ()
{
char a = 1;
Char c[] = "1234567890";
Char *p = "1234567890";
A = c[1];
A = p[1];
Return
}
The corresponding assembly code
10:a = c[1];
00401067 8A 4D F1 mov cl,byte ptr [ebp-0fh]
0040106A 4D FC mov byte ptr [ebp-4],cl
11:a = p[1];
0040106D 8B-EC mov edx,dword ptr [ebp-14h]
00401070 8A mov al,byte ptr [edx+1]
00401073 FC mov byte ptr [ebp-4],al
The first reads the elements in the string directly into the register CL, and the second one reads the pointer value to
In edx, it is obviously slower to read the characters according to EdX.
Add:
The stack is the data structure provided by the machine system, the computer will support the stack at the bottom: allocate the address of the special register storage stack, and the stack stack has special
command execution, which determines thethe efficiency of the stack is relatively high。 The heap is provided by the C + + function library, and itsmechanism is very complex, for example, in order to allocate a piece of memory,
The library function searches the heap memory for an available space of sufficient size in accordance with a certain algorithm (the specific algorithm can refer to the data structure/operating system), if no
There is a sufficient amount of space (possibly due to too much memory fragmentation), it is possible to call system functions to increase the memory space of the program data segment, so there is a chance
To a sufficient amount of memory, and then return. ObviouslyThe heap is much less efficient than the stack.。
7. Allocation Method:
(1) The heap isDynamic Allocation, there is no statically allocated heap.
(2) There are two ways to allocate stacks: static allocation and dynamic allocation.static allocations are done by the compiler, such as the allocation of local variables。dynamically allocated by Alloca
Functions are allocated, but the dynamic allocation of stacks is different from the heap. It'sdynamic allocation is released by the compiler,No manual implementation。
Summary:
First, the heap is a dynamic allocation of malloc, similar to the global, static (immutable), the stack is the compiler to complete, the main storage is the thread, function, parameters, local variables (variable)
Second, the heap efficiency is low, but the use is convenient, the stack efficiency is high, but the program ape cannot control
The difference between heaps and stacks can also be seen in the following analogy:
Using stacks like we eat in a restaurant, just order (apply), pay, and eat (use), eat full
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 since
Small by degrees.
Using the heap is like making your own favorite dishes, more troublesome, but more in line with their own tastes, and free
The high degree.
Heap and Stack in C language 20160604