What is the Stack Overflow error ? Just to warm You, i'ts not an error that occurs in the most famous website for us programmers. This error is the reason why this website has this name.
What is a Stack:
Before we start to talk about this error, we need to know what is a STACK ? Stack is a simple Data Structure that consists in the first that is added will be the last to execute. Is something like a pile of chairs, if you want to store a chair, you can just put one above each other, but if You want to get one of them you cannot take one from the middle of the pile, you need to get the one that is on the top, this is the same thing with Stack, we call it FILO (First In Last Out)
Recursion
Now You already know what is a Stack so we can start to talk about the main subject of this post/article we're going to talk about Recursion, so what is Recursion ? Recursion is a way a little bit more elegant to make a loop, it consists in call a function inside the same function, something like this:
fun iAmARecursion(max: Int, start: Int) {
if (start == max) {
println("this is the end")
} else {
println(start)
iAmARecursion(max, start + 1)
}
}
As You can see here we have a function called iAmARecursion that receives 2 parameters max and start, and when max is equal than start we just print the phrase "this is the end" but when the start is not equals than max we call the function iAmARecursion again, but updating the value of the start adding 1 to them, so the function will run again but a different start, this will occur until the start reach the max, and after this we will receive our response. But why I need to know stack to understand recursion ? Good question Young Padawan , You need to know stack to understand how recursion will work behind the scene, let's get back to the pile of the chairs example, let's imagine, I want to get a chair, but I want the first one in the pile, the one who is in the first position below all the chairs. To do this I need to remove the last chair, and the chair below that, and the other one, and so on, to write that with loops like a while We can do this:
fun getTheChair(myChairPosition: Int, totalOfChairs: Int) {
var i = totalOfChairs
while(i > 0) {
if(myChairPosition == i) {
println("this is my chair")
break
} else {
i -= 1
}
}
}
Here we have a loop, that will run while I is bigger than 0, so when "myChairPosition" is equal than "i" we will print "this is my chair" , but if it is not we will just remove a chair using "i-=1". It's good ? It is, but we can make it more elegant, let's do it.
fun getTheChair(myChairPosition: Long, totalOfChairs: Long) {
if(myChairPosition == totalOfChairs) {
println("this is my chair")
} else {
getTheChair(myChairPosition, totalOfChairs - 1)
}
}
So much better, no ?! Now we have an elegant solution, every time that the totalOfChairs is bigger than myChairPosition we will call the getTheChair function again and just removing 1.
This is RECURSION guys, but now You probably are thinking why the name of this post is The Stack Overflow Error ? Ok, I will tell You, when we are working with Java, one of the most common reasons of this error is because we are using recursion, when we have a so big number and just choose to use a recursion, we start to put on the stack of the memory multiples calls for the function. In this case if we have a 1000000000 chairs for example, everytime that we call the function "getTheChair" the call would be stored in our memory and would be there until the last one ends to execute, so this can make our memory freaks out and will send to us an error called "Stack Overflow Error"
Exception in thread "main" java.lang.StackOverflowError
at FileKt.getTheChair (File.kt:21)
at FileKt.getTheChair (File.kt:21)
at FileKt.getTheChair (File.kt:21)
Thank You so Much to read till here, I hope you enjoyed this content and I hope to see You later.
Bye =]
Top comments (0)