Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack Overflow error vs. Infinite loop

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?

like image 579
Avi Pars Avatar asked Nov 30 '14 21:11

Avi Pars


People also ask

Does infinite loop causes stack overflow?

The most-common cause of stack overflow is excessively deep or infinite recursion, in which a function calls itself so many times that the space needed to store the variables and information associated with each call is more than can fit on the stack. An example of infinite recursion in C.

What is the error for infinite loop?

An infinite loop error describes a technical glitch that forces your computer to repeat the same actions over and over again. For example, you just restarted your device after you installed the latest OS version. But instead of booting up, your computer keeps on restarting.

Is an infinite loop a runtime error or logic error?

Coding an infinite loop is generally not a good idea, and it should never happen by accident. For now, any infinite loops are logic errors that must be fixed. If you notice an infinite loop as a result of testing your code, type CTRL-C (press the Ctrl key and the letter c at the same time) to get your program to stop.

Is it good to use infinite loop?

Infinite loops are useful if you want your script (or part of the code) to run endlessly, until a specific action is taken. Especially, when there can be more than one action that stops your script or block of code from working. In event-based programming paradigm endless loops are very common.


1 Answers

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.

like image 81
Marko Živanović Avatar answered Sep 25 '22 06:09

Marko Živanović