Questions tagged [stack]

A stack is a last in, first out (LIFO) abstract data type and data structure. For questions about the assembly call stack, use [stack-memory], [stack-pointer], and/or [stack-frame] instead. For questions about the Haskell build tool, use [haskell-stack] instead. For questions about C++ std::stack, use [stdstack] instead.

A stack is a data structure, often used as an abstract data type (ADT) in many programming languages, to store data in a last in, first out (LIFO) manner. You could imagine it like a stack of cards. If you put a card onto the top of the stack, and then take the top card off the stack, you'll get the same card you put onto it.

Putting data onto a stack is called pushing. Taking something from a stack is called popping. Checking what's on top of a stack without removing it is called peeking.

Here is an example program:

a = new stack()
a.push(5)
b = a.peek()
print(b)
a.push(6)
b = a.pop()
print(b)
b = a.pop()
print(b)

would give the output:

5
6
5

The is a memory region that's used to allocate space in LIFO order for the local variables of functions (and other things like return addresses and space to save call-preserved registers, all part of a function's ). It allows easy nesting of function calls (including for recursive functions). The current top-of-stack is usually tracked by a dedicated register. Different CPU architectures and calling conventions manage the stack differently, but it's common to push a return address as part of calling a function, building a . Returning pops the return address and jumps to it, allowing a function to return to wherever it was called from.

Related tags:

10896 questions
43
votes
11 answers

Why are stack overflows still a problem?

This question is mystifying me for years and considering this site's name, this is the place to ask. Why do we, programmers, still have this StackOverflow problem? Why in every major language does the thread stack memory have to be statically…
Rotsor
  • 13,655
  • 6
  • 43
  • 57
43
votes
12 answers

Basic Recursion, Check Balanced Parenthesis

I've written software in the past that uses a stack to check for balanced equations, but now I'm asked to write a similar algorithm recursively to check for properly nested brackets and parenthesis. Good examples: () [] () ([]()[]) Bad examples:…
pws5068
  • 2,224
  • 4
  • 35
  • 49
43
votes
9 answers

How to detect possible / potential stack overflow problems in a c / c++ program?

Is there a standard way to see how much stack space your app has and what the highest watermark for stack usage is during a run? Also in the dreaded case of actual overflow what happens? Does it crash, trigger an exception or signal? Is there a…
KPexEA
  • 16,560
  • 16
  • 61
  • 78
42
votes
5 answers

Why use a stack-oriented language?

I recently took a look at Factor, and the idea of having a language based around the concept of a stack is very interesting. (This was my first encounter with a stack-oriented language.) However, I don't see any practical advantages of such a…
Sasha Chedygov
  • 127,549
  • 26
  • 102
  • 115
42
votes
6 answers

Java Collections (LIFO Structure)

I am looking in the Collections framework of Java for a LIFO Structure (Stack) without any success. Basically I want a really simple stack; my perfect option would be a Deque, but I am in Java 1.5. I would like not to have to add another class to my…
David Santamaria
  • 8,671
  • 7
  • 33
  • 43
41
votes
13 answers

How to remove a stack item which is not on the top of the stack in C#

Unfortunately an item can only be removed from the stack by "pop". The stack has no "remove" method or something similar, but I have a stack (yes I need a stack!) from which I need to remove some elements between. Is there a trick to do this?
Enyra
  • 17,542
  • 12
  • 35
  • 44
41
votes
4 answers

How to check if a Stack is empty

Is there some other way, except Stack.Count() == 0, to check if a Stack is empty? Coming from C++/Java background where "stack" classes generally have some sort of dedicated "is empty" method like Java - Stack.empty.
BobsunShirov
  • 447
  • 1
  • 4
  • 6
40
votes
5 answers

Navigating Java call stack in Eclipse

In debuggers like GDB, when you stop at a breakpoint, you can easily move up the call stack and examine the relevant source and stack frame data. How do you do this in Eclipse?
Mark
  • 1,049
  • 4
  • 12
  • 14
40
votes
10 answers

Java Stack push() vs add()

I am trying to use Stack, but I am slightly confused by the terminology. I find that Stack class has only push(E e) as per Java doc. And has add(E e) and addAll(Collection c) as a inherited method from Vector class. Do they have the…
Kalyanaraman Santhanam
  • 1,371
  • 1
  • 18
  • 30
39
votes
6 answers

peek operation in stack using javascript

How can I get the first element from my stack here is my code var stack = []; stack.push(id1); stack.push(id2); I know there is something like peek in java. Is there any similar method in JS using which i can get the topmost element?
JustCurious
  • 780
  • 1
  • 10
  • 29
39
votes
4 answers

What is the difference between a segmentation fault and a stack overflow?

For example when we call say, a recursive function, the successive calls are stored in the stack. However, due to an error if it goes on infinitely the error is 'Segmentation fault' (as seen on GCC). Shouldn't it have been 'stack-overflow'? What…
AruniRC
  • 5,070
  • 7
  • 43
  • 73
39
votes
2 answers

C++: Stack's push() vs emplace()

Trying to understand the difference between using push() or emplace() for std::stack. I was thinking that if I create a std::stack, then I'd use push() because integer is a primitive type and there is nothing for emplace() to construct.…
Joshua Oliphant
  • 902
  • 3
  • 10
  • 17
39
votes
31 answers

Parenthesis/Brackets Matching using Stack algorithm

For example if the parenthesis/brackets is matching in the following: ({}) (()){}() () and so on but if the parenthesis/brackets is not matching it should return false, eg: {} ({}( ){}) (() and so on. Can you please check this code? public static…
Shuvo0o
  • 489
  • 1
  • 8
  • 16
39
votes
10 answers

Order of local variable allocation on the stack

Take a look at these two functions: void function1() { int x; int y; int z; int *ret; } void function2() { char buffer1[4]; char buffer2[4]; char buffer3[4]; int *ret; } If I break at function1() in gdb, and print…
David
  • 1,101
  • 2
  • 9
  • 11
38
votes
3 answers

Escape analysis in Java

As far as I know the JVM uses escape analysis for some performance optimisations like lock coarsening and lock elision. I'm interested if there is a possibility for the JVM to decide that any particular object can be allocated on stack using escape…
Denis Bazhenov
  • 9,680
  • 8
  • 43
  • 65