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()
b = a.peek()
b = a.pop()
b = a.pop()

would give the output:


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:

10257 questions
30 answers

What and where are the stack and heap?

Programming language books explain that value types are created on the stack, and reference types are created on the heap, without explaining what these two things are. I haven't read a clear explanation of this. I understand what a stack is. But,…
29 answers

How do you implement a Stack and a Queue in JavaScript?

What is the best way to implement a Stack and a Queue in JavaScript? I'm looking to do the shunting-yard algorithm and I'm going to need these data-structures.
  • 61,803
  • 51
  • 116
  • 150
5 answers

Why is Java Vector (and Stack) class considered obsolete or deprecated?

Why is Java Vector considered a legacy class, obsolete or deprecated? Isn't its use valid when working with concurrency? And if I don't want to manually synchronize objects and just want to use a thread-safe collection without needing to make fresh…
  • 10,698
  • 9
  • 39
  • 55
23 answers

Which is faster: Stack allocation or Heap allocation

This question may sound fairly elementary, but this is a debate I had with another developer I work with. I was taking care to stack allocate things where I could, instead of heap allocating them. He was talking to me and watching over my shoulder…
  • 25,118
  • 23
  • 74
  • 87
24 answers

Why is the use of alloca() not considered good practice?

alloca() allocates memory on the stack rather than on the heap, as in the case of malloc(). So, when I return from the routine the memory is freed. So, actually this solves my problem of freeing up dynamically allocated memory. Freeing of memory…
  • 5,389
  • 3
  • 20
  • 18
21 answers

How to implement a queue using two stacks?

Suppose we have two stacks and no other temporary variable. Is to possible to "construct" a queue data structure using only the two stacks?
  • 14,341
  • 8
  • 22
  • 14
38 answers

Android: Clear the back stack

In Android I have some activities, let's say A, B, C. In A, I use this code to open B: Intent intent = new Intent(this, B.class); startActivity(intent); In B, I use this code to open C: Intent intent = new Intent(this,…
  • 7,052
  • 9
  • 37
  • 45
10 answers

Stack smashing detected

I am executing my a.out file. After execution the program runs for some time then exits with the message: **** stack smashing detected ***: ./a.out terminated* *======= Backtrace:…
Biswajyoti Das
  • 7,011
  • 11
  • 32
  • 26
7 answers

Does Python optimize tail recursion?

I have the following piece of code which fails with the following error: RuntimeError: maximum recursion depth exceeded I attempted to rewrite this to allow for tail recursion optimization (TCO). I believe that this code should have been…
Jordan Mack
  • 7,113
  • 6
  • 28
  • 29
14 answers

Java ArrayList how to add elements at the beginning

I need to add elements to an ArrayList queue whatever, but when I call the function to add an element, I want it to add the element at the beginning of the array (so it has the lowest index) and if the array has 10 elements adding a new results in…
  • 4,362
  • 7
  • 26
  • 27
12 answers

What is stack unwinding?

What is stack unwinding? Searched through but couldn't find enlightening answer!
Rajendra Uppal
  • 17,582
  • 15
  • 57
  • 57
6 answers

Why should I use Deque over Stack?

I need a Stack data structure for my use case. I should be able to push items into the data structure and I only want to retrieve the last item from the Stack. The JavaDoc for Stack says : A more complete and consistent set of LIFO stack operations…
  • 24,931
  • 41
  • 141
  • 216
9 answers

Stack, Static, and Heap in C++

I've searched, but I've not understood very well these three concepts. When do I have to use dynamic allocation (in the heap) and what's its real advantage? What are the problems of static and stack? Could I write an entire application without…
  • 4,584
  • 8
  • 28
  • 26
9 answers

In C, do braces act as a stack frame?

If I create a variable within a new set of curly braces, is that variable popped off the stack on the closing brace, or does it hang out until the end of the function? For example: void foo() { int c[100]; { int d[200]; } //code…
  • 214,337
  • 157
  • 462
  • 666
26 answers

Implement Stack using Two Queues

A similar question was asked earlier there, but the question here is the reverse of it, using two queues as a stack. The question... Given two queues with their standard operations (enqueue, dequeue, isempty, size), implement a stack with its…
  • 2,894
  • 3
  • 18
  • 13
2 3
99 100