Memory allocation program

Stack by the operating system releases automatically assigned  values for parameters, local variable storage function and the like, which operate in a manner similar to the data structure of the stack

1. The function of the local variables defined in the order defined in the order successively pushed onto the stack, there will be no other variables between adjacent address that is variable.

2. The memory address stack growth direction opposite to the stack, from high in the end, the variable address defined by the first variable defined below

3. Stack stored in the data life cycle is completed with the execution of the function ends

File by the assigned programmer release , if the programmer does not release, recovered at the end of the program by the OS, it touches similar distribution list

1. heap memory address stack growth direction opposite from low to high, but it should be noted that, after the application behind memory space of the memory space is not necessarily prior application

The reason is that once the first application memory space is released, the application will use memory space previously freed memory, which has led to the allocated memory space does not exist in the address have relations

 

About heap memory space allocation process:

1. The operating system has a linked list of free memory address of record

2. When the system receives the application process, will traverse the linked list, find the first space is larger than the requested node heap space

3. Remove the node from the linked list of free nodes, and the node allocated to the program space

For most systems, this will first address memory space record size of this allocation, so that the code in the delete statement to release this memory space correctly.

Since the size of the size of the heap node found is not necessarily exactly equal to the application, the system will automatically that part of the excess back into the free list .

 

Heap and stack difference:
heap and stack actually are two management operating system process memory space, there are several major differences are as follows:
(1) a different management style . Stack is automatically assigned by the operating system release, we do not need to manually control; heap of work release and controlled by the application programmer, prone to memory leaks;

(2) spatially different sizes . Each process has a stack size is much smaller than the size of the heap. In theory, the programmer can apply heap size of the virtual memory size, process stack size of 64bits Windows default 1MB, 64bits of Linux default 10MB;

(3) different growth directions . Growth of the pile upwards, from low to high memory address; down the stack growth direction, from highest to lowest memory address.

(4) a different distribution . Heap is allocated dynamically, not statically allocated heap.

        There are two kinds of stack allocation:

              Static allocation and dynamic allocation. Static allocation is done by the operating system, such as the allocation of local variables.

              Dynamically allocated by alloca allocation function, but dynamic stack allocation and heap are different, his dynamic allocation is released by the operating system, we do not need to manually implement

Different (5) distribution efficiency.

         Stack is automatically assigned by the operating system, will provide support for the stacks at the hardware level: assign a special register contains the address of the stack, the stack has a special push instruction execution, which determines the efficiency of the stack is relatively high.

         The stack is provided by the C / C ++ library function or operator to complete the application and management, more complex mechanism, prone to frequent memory allocation memory fragmentation. Obviously, heap much lower efficiency than the stack.

Different (6) to store content. The contents of the stack for the function return address, parameters, local variables and registers and the like. When the main function calls another function when you want to save the current function execution breakpoints, you need to use the stack to achieve,

        Drawing is the first address of the next statement of the main function, i.e., the extended content pointer register (the EIP), and the bottom address of the current stack frame,

        I.e. extended base pointer register contents (EBP), and then followed by the called function arguments, etc., under normal circumstances is pushed onto the stack from right to left,

        Followed by the local variable called function, note that the static variable is stored in the data segment or the BSS is not pushed on. The opposite order of the stack,

        The final point to address the stack at the main function of a statement, the main program and from the location and begin execution. Stack, generally use top of the stack space to store a byte size of the heap, the heap particular content stored by the programmer is populated.

From the above it can be seen compared to heap and stack,

Heap: due to the large malloc () / free () or new / delete use, likely to cause a large amount of memory fragmentation, and may lead to user mode and kernel mode switching, the less efficient

Stack: in the program is widely used, the most common procedure is to call the function implemented by the stack function return address, EBP, arguments and local variables are used to store a stack of way.

      Although the stack has many benefits, but because it is not as flexible and heap compared to sometimes allocate a lot of memory space, mainly by heap

 

Whether heap or stack memory usage when we need to prevent illegal cross-border, illegal memory access violation may lead to the destruction of the program heap, stack data, ranging from lead to run in an indeterminate state, can not get the expected results, while in cause the program to crash an exception, these are our programming should pay attention to when dealing with memory problems

References:

https://blog.csdn.net/K346K346/article/details/80849966

Guess you like

Origin www.cnblogs.com/allenhaozi/p/11109788.html