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
62
votes
4 answers

C# Recursion Depth - How Deep can you go

Is there any control how much you can Recursively call something? From a basic test program I get a recursion depth of just over 18k which depends on the stacksize.... is there a way to set up a chunk of memory (perhaps a thread) with a massive…
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
61
votes
4 answers

"enter" vs "push ebp; mov ebp, esp; sub esp, imm" and "leave" vs "mov esp, ebp; pop ebp"

What is the difference between the enter and push ebp mov ebp, esp sub esp, imm instructions? Is there a performance difference? If so, which is faster and why do compilers always use the latter? Similarly with the leave and mov esp, ebp pop …
小太郎
  • 5,510
  • 6
  • 37
  • 48
61
votes
10 answers

numpy-equivalent of list.pop?

Is there a numpy method which is equivalent to the builtin pop for python lists? Popping obviously doesn't work on numpy arrays, and I want to avoid a list conversion.
Anton Alice
  • 723
  • 1
  • 6
  • 10
61
votes
3 answers

What does "ulimit -s unlimited" do?

There are understandably many related questions on stack allocation What and where are the stack and heap? Why is there a limit on the stack size? Size of stack and heap memory However on various *nix machines I can issue the bash command ulimit -s…
Brian Hawkins
  • 2,813
  • 2
  • 25
  • 17
59
votes
7 answers

How can I use stack in Kotlin?

How can I use Stack (from java) in Kotlin? Or theres any other alternative? I'm trying to convert list to Stack
user6556881
58
votes
4 answers

Stack capacity in C#

Could someone tell me what the stack capacity is in C#. I am trying to form a 3D mesh closed object using an array of 30,000 items.
George ARKIN
58
votes
15 answers

Stack and Queue, Why?

Why and when should I use stack or queue data structures instead of arrays/lists? Can you please show an example for a state thats it'll be better if you'll use stack or queue?
Alon Gubkin
  • 56,458
  • 54
  • 195
  • 288
57
votes
19 answers

How to implement 3 stacks with one array?

Sometimes, I come across the following interview question: How to implement 3 stacks with one array ? Of course, any static allocation is not a solution.
Michael
  • 41,026
  • 70
  • 193
  • 341
57
votes
7 answers

Android App Restarts upon Crash/force close

My android app is getting restarted after force close, through my entire application which consist of 20 activities, I am relying on static data created on a main activity. So once the app is getting crashed all my static data is getting lost and…
Techfist
  • 4,314
  • 6
  • 22
  • 32
56
votes
4 answers

Why does the stack address grow towards decreasing memory addresses?

I read in text books that the stack grows by decreasing memory address; that is, from higher address to lower address. It may be a bad question, but I didn't get the concept right. Can you explain?
Jestin Joy
  • 3,711
  • 4
  • 19
  • 14
55
votes
10 answers

Stack overflows from deep recursion in Java?

After some experience with functional languages, I'm starting to use recursion more in Java - But the language seems to have a relatively shallow call stack of about 1000. Is there a way to make the call stack bigger? Like can I make functions that…
Lucky
  • 4,787
  • 9
  • 40
  • 50
54
votes
4 answers

Stack with find-min/find-max more efficient than O(n)?

I am interested in creating a Java data structure similar to a stack that supports the following operations as efficiently as possible: Push, which adds a new element atop the stack, Pop, which removes the top element of the stack, Find-Max, which…
Techkriti
  • 541
  • 1
  • 5
  • 3
54
votes
10 answers

How to clear the Android Stack of activities?

I have an application with several Activities in Android and I want the user to be able to log-out by pressing a menu button. The problem I have is that A) Android doesn't let you terminate the application and B) even when I send the user to the…
Totic
  • 1,281
  • 1
  • 10
  • 21
54
votes
9 answers

What is a stack pointer used for in microprocessors?

I am preparing for a microprocessor exam. If the use of a program counter is to hold the address of the next instruction, what is use of stack pointer?
KAR
54
votes
20 answers

Weird MSC 8.0 error: "The value of ESP was not properly saved across a function call..."

We recently attempted to break apart some of our Visual Studio projects into libraries, and everything seemed to compile and build fine in a test project with one of the library projects as a dependency. However, attempting to run the application…
user11180
  • 751
  • 2
  • 6
  • 5