What is a stack overflow?

Profile picture

VERIFIED

Verified tutor tick

Fares Alaboud

Computer science university teaching assistant. Taught 200+ students so far πŸŽ“

Although this question is marked as general, I assume you mean this question in the context of computer programming. A stack, in this context, is the last in, first out buffer you place data while your program runs. Last in, first out (LIFO) means that the last thing you put in is always the first thing you get back out. Adding an item to the stack is called pushing, taking an item from the stack is called popping. If you push 2 items on the stack, 'A' and then 'B', then the first thing you pop off the stack will be 'B', and the next thing is 'A'. Think of a stack of plates. If you add a plate to the stack, it is at the bottom. If you add a second plate it is at the top. You must first remove (pop) the second plate in order to pop the first one. When you call a function in your code, the next instruction after the function call is stored on the stack, and any storage space that might be overwritten by the function call. The function you call might use up more stack for its own local variables. When it's done, it frees up the local variable stack space it used, then returns to the previous function. A stack overflow is when you've used up more memory for the stack than your program was supposed to use. Many programmers make this mistake by calling function A that then calls function B, that then calls function C, that then calls function A. It might work most of the time, but just once the wrong input will cause it to go in that circle forever until the computer recognizes that the stack is overblown. Recursive functions are also a cause for this, but if you're writing recursively (ie, your function calls itself) then you need to be aware of this and use static/global variables to prevent infinite recursion. You should look at your call graph (a tree structure that shows from your main method what each function calls) to see how deep your function calls go, and to detect cycles and recursion that are not intended. This answer was adapted from an answer here (from a website called stackoverflow!): https://stackoverflow.com/questions/26158/how-does-a-stack-overflow-occur-and-how-do-you-prevent-it

Fares also answered

Asked in Computer Science πŸ–₯

ASKED BY PRISCILLA

COMPUTER SCIENCE πŸ–₯

What should you expect when you study computer science at university?

Let me begin by saying Computer Science is a great degree and is becoming evermore in the spotlight with the advent of AI, Machine Learning and Bloc...

ASKED BY PRISCILLA

COMPUTER SCIENCE πŸ–₯

What are the typical lecture hours when studying computer science at university?

I studied Computer Science at King’s College London so my answer may be different for students who studied elsewhere. In saying that, a fair few of ...

ASKED BY HAMZA

COMPUTER SCIENCE πŸ–₯

What is 1011 when referring to computing?

1011 is a number represented in binary. Computers can only understand and process binary numbers. Anything you ever do on a computer will be convert...

ASKED BY LINH

COMPUTER SCIENCE πŸ–₯

How do I start learning React.js?

In my experience of learning a new framework and seeing how others have learnt (where they've succeeded and stumble), I'd recommend the following: -...

ASKED BY BONNIE

COMPUTER SCIENCE πŸ–₯

Video game producers often use stereotypical characters. Explain one reason why the producers use sterotypical characters?

It's the same reason you find stereotypical characters in movies & advertising. In my opinion, it happens for one of two reasons: 1. Unconscious ste...

ASKED BY IBHADE

COMPUTER SCIENCE πŸ–₯

Explain what a pathogen is ?

Me thinks this is a biology question πŸ˜› A pathogen is really a microorganism that can cause disease. Bacteria for example are microscopic organisms ...

ASKED BY ANNABEL

COMPUTER SCIENCE πŸ–₯

What is the ROM and what does it do?

ROM stands for Read only memory. It is one of the types of storage used in computers. Data stored in ROM does not get lost even after the computer i...

ASKED BY SELINA

COMPUTER SCIENCE πŸ–₯

For a computer science student, is it better to buy a Mac or Windows?

Buy a Mac, you won't regret it πŸ˜‰

ASKED BY DUAA

COMPUTER SCIENCE πŸ–₯

What is the features of the brush tool in photoshop?

The Brush tool is one of the main basic drawing tool. It works like a traditional brush, and you can apply the color using strokes. You can also dow...

ASKED BY SELINA

COMPUTER SCIENCE πŸ–₯

What is a stack overflow?

Although this question is marked as general, I assume you mean this question in the context of computer programming. A stack, in this context, is th...

Find me a tutor

We take your privacy seriously. View our policy.