Questions tagged [stack]

A stack is a last in, first out (LIFO) abstract data type and data structure. For questions about the call stack, use [callstack] or [stack-pointer] instead. For questions about the Haskell build tool, use [haskell-stack] instead. For questions about the standard stack in C++, 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 stack data structure that's used to keep track of local variables and allows easy nested 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. Returning pops the return address and jumps to it, allowing a function to return to wherever it was called from.

Related tags:

10392 questions
8542
votes
28 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,…
811
votes
25 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.
KingNestor
  • 59,315
  • 50
  • 115
  • 149
706
votes
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…
fjsj
  • 10,501
  • 9
  • 36
  • 54
520
votes
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…
Adam
  • 24,261
  • 23
  • 72
  • 87
437
votes
22 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…
Vaibhav
  • 5,189
  • 3
  • 19
  • 18
422
votes
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?
Nitin
  • 13,771
  • 8
  • 21
  • 14
348
votes
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,…
Martin
  • 6,904
  • 9
  • 36
  • 44
283
votes
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
  • 6,481
  • 10
  • 31
  • 26
229
votes
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
  • 6,473
  • 5
  • 27
  • 29
208
votes
11 answers

What is stack unwinding?

What is stack unwinding? Searched through but couldn't find enlightening answer!
Rajendra Uppal
  • 16,504
  • 15
  • 55
  • 57
207
votes
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…
ZeDonDino
  • 3,972
  • 7
  • 24
  • 27
195
votes
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…
Geek
  • 23,609
  • 39
  • 133
  • 212
173
votes
5 answers

When vectors are allocated, do they use memory on the heap or the stack?

Are all of the following statements true? vector vect; //allocates vect on stack and each of the Type (using std::allocator) also will be on the stack vector *vect = new vector; //allocates vect on heap and each of the Type will…
Phelodas
  • 2,903
  • 5
  • 18
  • 30
169
votes
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…
Hai
  • 4,454
  • 8
  • 27
  • 26
155
votes
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…
Claudiu
  • 206,738
  • 150
  • 445
  • 651
1
2 3
99 100