answersLogoWhite

0


Best Answer

A stack overflow occurs when the on--chip stack capacity is exceeded. This can be detected by a comparison of the top-pointer, the last-pointer and the index specified for a create-frame operation ( for push). Initially the top-pointer is set to 1 and the last-pointer to 0, placing a dummy element on the stack.

If an index specified for a read access is greater than the number of valid on--chip stack elements, a stack underflowoccurs

Queue overflow results from trying to add an element onto a full queue and queue underflow happens when trying to remove an element from an empty queue.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

ProfBot

2w ago

In a stack data structure, an overflow condition occurs when an attempt is made to push an element onto a full stack, resulting in a stack that exceeds its maximum capacity. This can lead to data loss or corruption. Conversely, an underflow condition occurs when an attempt is made to pop an element from an empty stack, resulting in an invalid operation. Proper error handling mechanisms should be implemented to prevent these conditions and ensure the integrity of the stack.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

A stack overflow typically occurs when a new element must be pushed onto a stack, but the stack doesn't have sufficient memory in which to place the new element. We can liken this to a cup; when we try to fill it with more water than it can physically hold it will overflow.

Stacks are normally dynamic structures that grow and shrink as elements are pushed and popped from them and therefore do not (or should not) suffer from stack overflows. We can still run into problems if there is insufficient memory in which to grow the stack, however that is another problem entirely; it has nothing to do with stack overflow -- it's a systemic memory allocation problem.

Stack overflows only occur in fixed-length stacks where a new element is pushed onto the stack when the stack is already full or there is insufficient capacity for the new element. The main problem here is what do we do with the new element? The best solution is to simply throw an exception and let the caller (the code that invoked the push operation) deal with the problem.

Stack overflows can also occur at the system level. Every thread of execution has its own thread-local call stack and each time we invoke a function we consume a stack frame that holds the function's local variables, formal arguments, return address and exception handling information. However, call stacks are fixed-length and thus have a limited capacity. When we overflow a call stack, the system generates a stack overflow error and terminates the process because there's simply no way to recover from this; we've invoked a function for which a stack frame cannot be generated. Recursive functions are particularly susceptible to stack overflow errors.

A stack underflow (or underrun as it's more usually called) occurs when we try to pop an element from an empty stack. For example:

std::stack<int> stk;

stk.push (42);

stk.pop();

stk.pop(); // stack underflow...

We can guard against this by testing if the stack is empty before popping:

std::stack<int> stk; stk.push (42);

if (!stk.empty()) stk.pop(); // ok

if (!stk.empty()) stk.pop(); // stack underflow avoided...

However, this is often inefficient as an underrun is (or at least should be) a rare occurrence; an exceptional circumstance. Like all exceptions, we should provide an exception handler to deal with it:

std::stack<int> stk;

stk.push (42);

try {

stk.pop();

stk.pop();

}

catch (std::exception& e) {

// handle the exception...

}

Underruns often occur in data structures used for buffering. Typically we use buffers in order to synchronise two processes executing at different frequencies, typically where one process must operate at a fixed frequency. For example, when writing data to a DVD we can usually read data from the hard-drive much quicker than we can physically write it to DVD, so we use a buffer to "queue" the data that is waiting to be written. We begin writing when the buffer is full and as each element is popped from the front of the buffer to be written to the DVD, a new element is pushed onto the back of the buffer. The reading process may be interrupted briefly by other processes that require access to the hard-drive, but so long as the queue contains data waiting to be written this won't affect the writing process. An underrun will only occur if the reading process is interrupted to the extent the queue is emptied completely. While DVD hardware and software can guard against this to a certain degree, if there are too many underruns during a write, the DVD could be corrupted. Digital audio playback is another example; whenever an underrun occurs, we hear the audio stuttering.

Stacks aren't generally use for buffering as such because the data isn't being "queued" for processing in the order that it arrives; first-in, first-out (or FIFO). A stack is a last-in, first-out (or LIFO) structure and we usually use them in backtracking algorithms, however we must still guard against stack overflows and underruns or at least throw exceptions when they occur so the callers can deal with them in an appropriate manner.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are overflow and underflow conditions in stack?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the meaning of stack underflow?

Stack underflow occurs when an operation is attempted on an empty stack, resulting in an attempt to access a nonexistent element at the top of the stack. This can lead to errors or unexpected behavior in programs that rely on stack data structures. To prevent stack underflow, it is important to check the stack's current size before performing operations that could potentially lead to underflow.


What is the condition for the overflow in the linked lists?

In linked list if there is no any element inside it than we can say linked list is underflow.


What is overflow and underflow in queue?

In C Programming arrays are given a size, which is the number of elements in the array. Space is allocated for an array in memory. The is the only memory that the array is supposed to use. C is a programming language that has very little restrictions regarding what it will allow you to do. If you try to write to a memory location that is not supposed to be written over, C will let you, however sometimes the operating system will prevent this. When an array writes past where it is supposed to this is called array overflow. For example. If is an array is designed to hold 50 integers and the program adds 51 integers then the extra integer is written at the end of the array and had to be written in memory not designated for the array.


Why doesn't this Python code do what it's supposed to do - see Discussion?

Ask on Stack Overflow. Wiki Answers is not for pasting code


What is meant by overflow in a digital circuit?

A overflow is a condition in which a calculation produces a unit of data too large to be stored in the location alloted to it. An overlow cannot happen when two numbers of opposite sign are added. An overflow may occur in an addition of binary numbers if the augend and addend are both positive or negative.

Related questions

How do you clear a stack overflow?

How do you clear a stack overflow


When was Stack Overflow created?

Stack Overflow was created in 2008.


What are some common errors detected by the CPU?

Fixed point overflow, Floating point overflow, Floating point underflow, etc.


Stack overflow at line 597?

how to fix stack overflow at line 597


What is a message from webpage stack overflow at line 158?

Thuynch2003@yahoo.com stack overflow at line 158


What is the meaning of stack-based buffer overflow?

A buffer overflow occurs when you put more stuff into it than it can hold. For a stack, it means you put or pushed onto the stack more information than the size of the stack.If I have a stack that can hold 10 entries, then putting 11 in the stack will overflow it.


How do you fix the problem of a stack overflow?

(whodatrml1@yahoo.com). How do i solve problem of stack overflow? Can I do without spending money? Thank you


How do you stop stack overflow line 160?

restart your computer, it will clear the overcommitted stack


What is Stack overflow at line?

A stack overflow is a type of buffer overflow in which an array writes memory outside of the array boundaries. The keyword here is "stack". The stack is a section in memory in which local variables and other program data are kept for future reference. When the stack gets overflown, adjacent program memory, such as variables, pointers, etc, will be overwritten and cause your program to crash.


How do fixed a stack overflow at line 203?

Allocate more memory to the stack or write code that does not leave stuff on the stack.


What is the meaning of a stack overflow?

A stack overflow is a programming term used to identify when a function tries to access memory from a stack that does not exist. A stack, such as a queue or array, contains a limited number of memory spaces set aside when it is created. For example, if an array has 8 objects in it and a function tried to access an item at slot nine, which doesn't exist, it would cause a stack overflow.


How do you fix stack overflow on a computer?

A quick fix for stack overflow problems is to run a registry cleaning program. There are a lot of them out there so just search for Registry Cleaner.