What are the common causes of stack overflow?

A stack is an abstract data type that is often used in computer science. Objects in the stack have a property: the last object put on the stack is always the first to be taken out, this property is often called a last-in-first-out (LIFO) queue. There are some operations defined on the stack. The two most important are PUSH and POP. The PUSH operation adds an element to the top of the stack. The POP operation is the opposite, removing an element from the top of the stack and reducing the size of the stack by one.

The stack overflow is caused by too many function calls, the call stack cannot accommodate the return address of these calls, and it is generally generated in recursion. Stack overflows are most likely caused by infinite recursion (Infinite recursion), but it could also simply be too many stack levels.

The general reasons for overflow are as follows:

1. The function call level is too deep. When a function is called recursively, the system must keep saving the scene and generated variables of the function call in the stack. If the recursive call is too deep, the stack will overflow, and the recursion cannot return at this time. Furthermore, when the function call level is too deep, the stack may not be able to accommodate the return addresses of these calls, resulting in stack overflow.

2. The dynamic application space is not released after use. Since there is no automatic recycling mechanism for garbage resources in C language, the program needs to actively release the dynamic address space that is no longer used. The dynamic space requested uses heap space, and the use of dynamic space will not cause heap overflow.

3. Array access out of bounds. The C language does not provide an array subscript out-of-bounds check. If the array subscript access exceeds the range of the array in the program, a memory access error may occur during operation.

4. Pointer illegal access. The pointer holds an illegal address, and a memory access error will occur when accessing the pointed address through such a pointer.

Heap overflow: constantly new an object, always create new objects,

Stack overflow: infinite loop or too deep recursion, the reason for recursion may be too large, or it may not terminate.

Usually "stack overflow" refers to "overflow of the call stack". It can be difficult to explain the call stack in layman's terms because it involves knowledge of many other computer architectures. And this answer simply explains the characteristics of the data structure of the stack-first in, last out/last in, first out. Overflow means that the data structure is overflowing and cannot store more data. This happens with other data structures as well. Even if the data structure is not fixed capacity, but scalable, there is still a chance of overflow under limited memory space.

In addition, many times, the occurrence of "call stack overflow" is related to recursion. We can change some recursive implementations to iteration (iteration), but sometimes we still have to have a custom stack data structure, such as depth-first search (Depth-First Search, DFS) for trees. Custom stacks are also possible to overflow.

Therefore, although stack overflow often refers to call stack overflow, it is essentially just a data structure overflow situation

Friends who are interested in the embedded Internet of Things can learn more about relevant information. (look over)

 

Guess you like

Origin blog.csdn.net/m0_70888041/article/details/130556197