Logo

dev-resources.site

for different kinds of informations.

The Stack Overflow Error

Published at
11/8/2024
Categories
computerscience
programmers
backend
algorithms
Author
jairo-dev-jr
Author
12 person written this
jairo-dev-jr
open
The Stack Overflow Error

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)

Stack example using pile

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)
    }
}
Enter fullscreen mode Exit fullscreen mode

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
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

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)
    }
}
Enter fullscreen mode Exit fullscreen mode

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) 
Enter fullscreen mode Exit fullscreen mode

Thank You so Much to read till here, I hope you enjoyed this content and I hope to see You later.
Bye =]

programmers Article's
30 articles in total
Favicon
AI and the Evolution of Coding: Will AI Tools Replace Programmers?
Favicon
Tech Stack 2025
Favicon
The Importance of Writing Articles as a Developer
Favicon
DeepSeek AI: A Comprehensive Guide for Programmers and Beyond
Favicon
The 12 Easiest Programming Languages to Learn
Favicon
Programcılığa Adım Atmak: Yeni Başlayanlar veya Başlamayı Düşünenler İçin Kısa Bir Kılavuz
Favicon
Web Development Trends to Watch in 2025 🌟
Favicon
Why soft skills are important for a programmer?
Favicon
Understanding the package.json
Favicon
7 Must-Have AI Tools Every Coder Should Know in 2025
Favicon
Global Talent: The Employer-Independent UK Visa
Favicon
Unlocking Creativity with C++: 14 Exciting Project Ideas for Programmers
Favicon
Best Programming Solutions
Favicon
How I Improved My Productivity as a Developer in 30 Days 🚀
Favicon
Рекрутеры — паразиты? Отказ на свою же должность в IT
Favicon
Keep-Alive Nedir ve Nasıl Etkinleştirilir?
Favicon
Discover the Best Programming Codes – No Signup or Fees Required!
Favicon
15 + 2 Instant Graphics Improvements For Web Programmers - For Easier Work Or For Your Single-Person Project
Favicon
Coding Tattoos
Favicon
Beginner-Friendly Machine Learning Tools to Start Your AI Journey
Favicon
The Stack Overflow Error
Favicon
Top 5 Programming Profiles: Racer, Coder, Maker, Joker, Tutor
Favicon
Web Development Services
Favicon
How to Create a Stylish Loader for Your Website
Favicon
Day 3: Modules and Pip | 100 Days Python
Favicon
Coding: Your Gateway to the Digital World
Favicon
How the Global Talent visa might make you layoff-proof in the UK 🇬🇧
Favicon
Feliz día de la programación
Favicon
Rust Cursive / TUI Crash Course + App Project
Favicon
The Danger Of Play Store Disclosing Developers’ Personal Info!

Featured ones: