When does the stack really overflow?

后端 未结 6 1551
别跟我提以往
别跟我提以往 2020-12-17 05:35

Is infinite recursion the only case or can it happen for other reasons? Doesn\'t the stack size grow as needed same as heap?

Sorry if this question has been asked be

6条回答
  •  爱一瞬间的悲伤
    2020-12-17 05:49

    The stack overflows when the stack pointer is pushed out of the memory block the operating system has allocated for the stack. Some operating systems will resize the stack as it grows (IIRC Linux does this) while in others the stack size is fixed at the start of the process or thread (IIRC Windows does this).

    Possible reasons for overflowing the stack:

    • An unbounded number of stack frames (e.g. from unbounded recursion)
    • Attempting to allocate large blocks from the stack
    • Buffer overflows for buffers allocated on the stack

    There are probably other reasons as well that I can't think of off the top of my head.

提交回复
热议问题