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
3
votes
1 answer

How to view the current stack size of a windows thread when it overflows

I have a process that is overflowing the stack when run from within an IIS process, but works fine when run on its own. I suspect that on its own it gets the default 1MB stack, but within IIS gets somewhat less. To avoid messing with the IIS worker…
Matt T
  • 607
  • 3
  • 10
3
votes
1 answer

n-queens puzzle in java using stacks and backtracking

This is my code for the n-queens problem in java. However, the output is 0 (the number of solutions to 8 queens in this case) when it should be 92. We're supposed to only use stacks and backtracking (no recursion!!). I'm really stuck! any help would…
mm93
  • 31
  • 1
  • 2
3
votes
2 answers

Generic "classes" inside R

I have written a stack "class" with the following functions: add, push, pop, size, isEmpty, clear (and some more). I'd like to use this "class" as a generic in R, so I may create multiple instances of stacks within my script. How do I go about…
laemtao
  • 147
  • 11
3
votes
2 answers

Retrieve function call graph

Possible Duplicate: Tools to get a pictorial function call graph of code I wrote a C++ function long time back in a dll (on windows) ... Some wrote api , calling my function using function pointers and some just called it .. I want to know…
MAG
  • 2,841
  • 6
  • 27
  • 47
3
votes
2 answers

c printf function dynamic type determination

I am trying to implement a generic stack in c using void pointer to point to the data. the structure looks like this struct record{ void* data; struct record* previousRecord; }; where void pointer data is a pointer to the data a stack position…
sidharth sharma
  • 3,025
  • 6
  • 23
  • 20
3
votes
1 answer

How to set up a new stack segment for programs in x86 to separate from my kernel?

I am developing a 32 bit OS and I need to be able to set up NEW data segments/stack segments for programs. However I can not find ANY good information about how to use these segment registers in protected mode. I really need to be able to set up a…
user1454902
  • 750
  • 9
  • 24
3
votes
2 answers

c++ "Run-Time Check Failure #2 - Stack around the variable 'board' was corrupted”

debug : Run-Time Check Failure #2 - Stack around the variable 'board' was corrupted. The program '[5516] a12aa.exe: Native' has exited with code 0 (0x0). edit: Oh I didn't mention that happens only in gamemode 1 (playing aginst the computer) in game…
user1951263
  • 41
  • 1
  • 6
3
votes
4 answers

why stack is called abstract data type.If stack is adt , then how to implement stack?

Stack is called as abstract data type, which is nothing but an interface.Then why stack comes under data structures topic.Is this a data structure or abstract data type?Is both are same or different?
user1849655
  • 53
  • 1
  • 1
  • 6
3
votes
3 answers

Remove ViewController from stack

In our App we have a log-in ViewController A. On user log-in, a request navigate is automatically called to navigate to the next ViewController B. However when this is done we want to remove the log-in ViewController A from the stack so the user…
Bjarke
  • 1,283
  • 11
  • 36
3
votes
1 answer

Raising Image with Tkinter and Python

I have several images that I want to layer in a different order, than the order in which they were created. I am using Python with Tkinter and was wondering if someone could help me with this. The order that I create the images is: #Using…
ben
  • 665
  • 4
  • 10
  • 16
3
votes
2 answers

Infix Calculator Expression Parser

How do I parse and evaluate expressions in an infix calculator grammar? I thought of two ways. The 1st involves using two stacks. One is for numbers and the other is for operators, and I would assess the operator precedence and association in order…
Painguy
  • 565
  • 6
  • 13
  • 25
3
votes
1 answer

JVMTI profiling: calculating the amount of stack memory

I am working on a JVMTI agent that monitors memory usage of an application. I have managed to monitor memory allocations on the heap using bytecode instrumentation so that a native method gets invoked whenever a new object/array gets allocated. But…
P.M.
  • 98
  • 5
3
votes
3 answers

Clear all activities

I have 4 activities: A, B, C and D. And one more: E. Thre is no problem when y start a new activity between A, B, C and D. But in some points of these (for example, when a login is successful) we want to start activity E but clear all A, B, C and D…
LopezAgrela
  • 548
  • 1
  • 4
  • 12
3
votes
5 answers

Why an uninitialized variable in C still produces output

I'm trying to figure out why even though the local variable i is never intialized in this C program, many systems will print out 0 1 2 3 4 5 6 7 8 9 Can someone explain why this is? Any help is appreciated! void foo() { int i; printf("%d ",…
Joe Crawley
  • 725
  • 6
  • 18
3
votes
4 answers

Java java.util.Stack multipop

i'm using java.util.Stack but i'm missing a multipop stack.pop(10); which should give me a list of 10 (or less is the stack does not contain enough) items from the stack (and remove them from the stack). Is there any standard class in java? or have…
wutzebaer
  • 14,365
  • 19
  • 99
  • 170
1 2 3
99
100