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
152
votes
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…
TechTravelThink
  • 3,014
  • 3
  • 20
  • 13
146
votes
11 answers

Android: Clear Activity Stack

I'm having several activities in my application. and flow is very complicated. When I click the Logout application navigates to login Screen and from there user can exit by cancel button (calling system.exit(0) ) when I exit or back button, the…
Jay Mayu
  • 17,023
  • 32
  • 114
  • 148
143
votes
9 answers

Java / Android - How to print out a full stack trace?

In Android (Java) how do I print out a full stack trace? If my application crashes from nullPointerException or something, it prints out a (almost) full stack trace like so: java.io.IOException: Attempted read from closed…
Jake Wilson
  • 88,616
  • 93
  • 252
  • 370
140
votes
2 answers

Why is stack size in C# exactly 1 MB?

Today's PCs have a large amount of physical RAM but still, the stack size of C# is only 1 MB for 32-bit processes and 4 MB for 64-bit processes (Stack capacity in C#). Why the stack size in CLR is still so limited? And why is it exactly 1 MB (4 MB)…
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
138
votes
12 answers

what is the basic difference between stack and queue?

What is the basic difference between stack and queue?? Please help me i am unable to find the difference. How do you differentiate a stack and a queue? I searched for the answer in various links and found this answer.. In high level programming, a…
Harish Rachakonda
  • 1,531
  • 3
  • 12
  • 7
130
votes
8 answers

Do threads have a distinct heap?

As far as I know each thread gets a distinct stack when the thread is created by the operating system. I wonder if each thread has a heap distinct to itself also?
user188276
126
votes
10 answers

Proper stack and heap usage in C++?

I've been programming for a while but It's been mostly Java and C#. I've never actually had to manage memory on my own. I recently began programming in C++ and I'm a little confused as to when I should store things on the stack and when to store…
Alexander
  • 3,037
  • 6
  • 24
  • 17
125
votes
31 answers

design a stack such that getMinimum( ) should be O(1)

This is an interview question. You need to design a stack which holds an integer value such that getMinimum() function should return the minimum element in the stack. For example: case #1 5 ← TOP 1 4 6 2 When getMinimum() is called it should…
Ganesh M
  • 3,666
  • 8
  • 27
  • 25
116
votes
9 answers

What is the direction of stack growth in most modern systems?

I am preparing some training materials in C and I want my examples to fit the typical stack model. What direction does a C stack grow in Linux, Windows, Mac OSX (PPC and x86), Solaris, and most recent Unixes?
Uri
  • 88,451
  • 51
  • 221
  • 321
116
votes
11 answers

Why do stacks typically grow downwards?

I know that in the architectures I'm personally familiar with (x86, 6502, etc), the stack typically grows downwards (i.e. every item pushed onto the stack results in a decremented SP, not an incremented one). I'm wondering about the historical…
Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
106
votes
2 answers

How do you extract local variable information (address and type) from a Delphi program or the compiler-generated debug info?

My goal is: Given a suspended thread in a Delphi-compiled 32 or 64-bit Windows program, to walk the stack (doable) Given stack entries, to enumerate the local variables in each method and their values. That is, at the very least, find their address…
David
  • 13,360
  • 7
  • 66
  • 130
100
votes
17 answers

How does the stack work in assembly language?

I'm currently trying to understand how the stack works, so I've decided teach myself some assembly language, I'm using this book: http://savannah.nongnu.org/projects/pgubook/ I'm using Gas and doing my development on Linux Mint. I'm a bit confused…
handles
  • 7,639
  • 17
  • 63
  • 85
100
votes
15 answers

Does stack grow upward or downward?

I have this piece of code in C: int q = 10; int s = 5; int a[3]; printf("Address of a: %d\n", (int)a); printf("Address of a[1]: %d\n", (int)&a[1]); printf("Address of a[2]: %d\n", (int)&a[2]); printf("Address of q: %d\n", …
user188276
93
votes
4 answers

How can I examine the stack frame with GDB?

Right now I've been using GDB to disassemble a binary file and check out different registers and whatnot. Is there an easy command to examine everything on the stack? Can this be limited to everything in a function?
GetOffMyLawn
  • 1,362
  • 4
  • 14
  • 21
91
votes
2 answers

What are SP (stack) and LR in ARM?

I am reading definitions over and over again and I still not getting what are SP and LR in ARM? I understand PC (it shows next instruction's address), SP and LR probably are similar, but I just don't get what it is. Could you please help me? edit:…
good_evening
  • 21,085
  • 65
  • 193
  • 298