Stack Overflow error vs. Infinite loop
Asked Answered
D

3

7

I know what an Infinite Loop error is. Is a stack overflow error the same thing. If not, what is the difference?

Can you give example code as well?

Diehl answered 30/11, 2014 at 21:58 Comment(1)
en.wikipedia.org/wiki/Buffer_overflowStratification
P
7

If, instead of infinite loop, you have infinite (or very deep) recursion (function invoking itself), then you will get stack overflow. Whenever a function is invoked, some part of stack memory is consumed. Once all the stack is exhausted, you get - stack overflow error.

Pau answered 30/11, 2014 at 22:1 Comment(0)
A
3

These are not the same thing. Infinite loop error is dealing with iterative loops (no recursion), where as most stack overflow errors are dealing with recursion.

You should google "What is a stack overflow error":

The most common cause of StackOverFlowError is excessively deep or infinite recursion. In Java: There are two areas in memory the heap and stack. The stack memory is used to store local variables and function call, while heap memory is used to store objects in Java.

Arroyo answered 30/11, 2014 at 22:1 Comment(0)
K
0

Stack overflow error can also be caused by never ending function call loop.

A simple example in TypeScript:

function foo(a: number): number {
    const localFooValue = 12;
    return bar(localFooValue + a);
}

function bar(b: number): number {
    const localBarValue = 4;
    return foo(localBarValue * b);
}

The preceding code snippet creates stack overflow error, With each foo function calling bar function and vice-versa this causes infinite function call loop.

Knockwurst answered 4/8, 2023 at 12:18 Comment(1)
I am just demonstrating a new condition for stack overflow error it also can happen in any language, About the question age it's OK to answer a question with much more years than that if i am making a contribution someone might see it as useful.Knockwurst

© 2022 - 2025 — McMap. All rights reserved.