3. Overflow and underflow

Basic Problem with Stacks: Overflow and Underflow

A stack cannot grow indefinitely, because there is always a limit to memory.

stack structure

When it comes to its limit and yet another operation tries to PUSH data onto the stack then a 'stack overflow' occurs. When this happens it often causes the program to crash. It is up to the software programmer to ensure that the stack does not overflow.

If the stack is empty and yet a POP operation is attempted, this is called an 'stack underflow' and can cause a program to crash. The software programmer should check that the stack is not empty before attempting a POP operation.

A common reason for a stack overflow is when a faulty piece of code ends up calling itself n an endless loop :

procedure print(x)

This is called infinite recursion because there is no end condition in place to exit this part of the code.


Challenge see if you can find out one extra fact on this topic that we haven't already told you

Click on this link: using a stack for programming


Copyright © www.teach-ict.com