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
52
votes
5 answers

How to evaluate an infix expression in just one scan using stacks?

I want to know if there is a way to solve infix expressions in a single pass using 2 stacks? The stacks can be one for operator and the other for operands... The standard way to solve by shunt-yard algorithm is to convert the infix expression to…
nikoo28
  • 2,961
  • 1
  • 29
  • 39
52
votes
10 answers

iOS how to detect programmatically when top view controller is popped?

Suppose I have a nav controller stack with 2 view controllers: VC2 is on top and VC1 is underneath. Is there code I can include in VC1 that will detect that VC2 has just been popped off the stack? Since I'm trying to detect the popping of VC2…
JMLdev
  • 846
  • 2
  • 10
  • 21
51
votes
8 answers

Getting a stack overflow exception when declaring a large array

The following code is generating a stack overflow error for me int main(int argc, char* argv[]) { int sieve[2000000]; return 0; } How do I get around this? I am using Turbo C++ but would like to keep my code in C EDIT: Thanks for the…
Patrick McDonald
  • 64,141
  • 14
  • 108
  • 120
51
votes
4 answers

Why can't structs be declared as const?

They are immutable value types on the stack. What keeps me from having them a…
Lazlo
  • 8,518
  • 14
  • 77
  • 116
51
votes
5 answers

How to determine maximum stack usage?

What methods are available for determining the optimum stack size for embedded/memory constrained system? If it's too big then memory is wasted that could be used elsewhere. However, if it is too small then we get this website's namesake... To try…
Judge Maygarden
  • 26,961
  • 9
  • 82
  • 99
50
votes
9 answers

Declare variables at top of function or in separate scopes?

Which is preferred, method 1 or method 2? Method 1: LRESULT CALLBACK wpMainWindow(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { switch (msg) { case WM_PAINT: { HDC hdc; PAINTSTRUCT ps; …
Kaije
  • 2,631
  • 6
  • 38
  • 40
50
votes
6 answers

Stack allocation, padding, and alignment

I've been trying to gain a deeper understanding of how compilers generate machine code, and more specifically how GCC deals with the stack. In doing so I've been writing simple C programs, compiling them into assembly and trying my best to…
David
  • 1,101
  • 2
  • 9
  • 11
48
votes
16 answers

How can I remember which data structures are used by DFS and BFS?

I always mix up whether I use a stack or a queue for DFS or BFS. Can someone please provide some intuition about how to remember which algorithm uses which data structure?
captcadaver
  • 1,093
  • 2
  • 9
  • 7
47
votes
6 answers

How to get the caller class name inside a function of another class in python?

My objective is to stimulate a sequence diagram of an application for this I need the information about a caller and callee class names at runtime. I can successfully retrieve the caller function but not able to get a caller class name? #Scenario…
Kaushik
  • 1,264
  • 8
  • 20
  • 32
46
votes
6 answers

Stack-buffer based STL allocator?

I was wondering if it practicable to have an C++ standard library compliant allocator that uses a (fixed sized) buffer that lives on the stack. Somehow, it seems this question has not been ask this way yet on SO, although it may have been implicitly…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
45
votes
9 answers

Checking available stack size in C

I'm using MinGW with GCC 3.4.5 (mingw-special vista r3). My C application uses a lot of stack so I was wondering is there any way I can tell programatically how much stack is remaining so I can cleanly handle the situation if I find that I'm about…
Paul Hargreaves
  • 1,757
  • 3
  • 23
  • 23
45
votes
4 answers

Setting stacksize in a python script

I am converting a csh script to a python script. The script calls a memory-intensive executable which requires a very large stack, so the csh script sets the stacksize to unlimited: limit stacksize unlimited When I try to reproduce this script in…
marshall.ward
  • 6,758
  • 8
  • 35
  • 50
45
votes
8 answers

Is there a bug in java.util.Stack's Iterator?

Today I was trying to push in java.util.Stack class and then use the Iterator to iterate (without using pop) through the items. I was expecting LIFO property but got surprised. Here is the code that I was trying. import java.util.*; import…
vincent mathew
  • 4,278
  • 3
  • 27
  • 34
45
votes
4 answers

Store results of std::stack .pop() method into a variable

I'd like to do the following: std::stack s; int h = 0; s.push(2); h = s.pop(); Such as to have h hold the value 2. When I try my method, I get “void value not ignored as it ought to be”. Is this not the intention of the .pop() method? What is…
PinkElephantsOnParade
  • 6,452
  • 12
  • 53
  • 91
44
votes
2 answers

Removing range (tail) from a List

Is there an efficient method to remove a range - say the tail - of X elements from a List, e.g. LinkedList in Java? It is obviously possible to remove the last elements one by one, which should result in O(X) level performance. At least for…
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263