8

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?

1

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

1

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.