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
2 answers

Assembly instructions for a function having pointer as local variable

I am trying to understand how assembly is generated for c. I wrote a sample program and disassembled it for the same. int main() { int a = 100; } Assembly generated: pushq %rbp # movq %rsp, %rbp #, subq $48, %rsp #, call __main …
trialyogi
  • 41
  • 3
3
votes
1 answer

PostScript current graphics state implementation

I have a question concerning the current graphics state and the Graphics State Stack in PostScript. In my code I now have a Stack which represents the current graphics state. When I initialise my interpreter it creates a new GraphicsState object and…
juFo
  • 17,849
  • 10
  • 105
  • 142
3
votes
2 answers

Ada beginner Stack program

Basically, I have 2 files ( .adb and .ads). I am totally new to Ada and also how to compile 2 files. The program is of a basic stack implementation. I got this compile error when I compiled the .adb file. $ gcc -c…
thestralFeather7
  • 529
  • 2
  • 10
  • 28
3
votes
2 answers

C++ parenthesis matching application

I have to write a function that accepts a string and returns a bool. The string passed in is a series or different parenthesis opened or closed ex.({[]}) and returns whether the 'parens' are well-balanced. This has to be implemented by adding items…
FJam
  • 736
  • 3
  • 13
  • 32
3
votes
1 answer

If I have a C function and I'm debugging it with gdb, how do I find the return value of a function?

I have an assignment that asks me to find the return value of main though register inspection (we're learning gdb), how would I go about doing that?
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
3
votes
4 answers

Pushing to a stack containing ONLY unique values in C

I've implemented a stack with pointers, that works like it's suppose too. Now, I need it push to the stack, without it pushing a duplicate. For example, if I push '2' into the stack, pushing another '2' will still result with only one '2' in the…
tomato
  • 831
  • 5
  • 15
  • 33
3
votes
5 answers

how to reverse a stack

I have an assignment where I am suppose to take a single stack, show the output and then reverse it to show the output. Its suppose to look like this Stack: 262 115 74 26 34 243 22 734 113 121 Stack Reversed: 121 113 734 22 243 34 26 74 115…
Gavon Black
  • 33
  • 1
  • 1
  • 4
3
votes
1 answer

Memory Access Permission, Need to execute code from stack. How to verify system permissions?

Question- Is there a command on Linux systems to see if execution from the stack is allowed? Background- Doing a homework assignment that requires a buffer overflow, injecting code into the stack, and overwriting a return address that will set the…
jeff
  • 162
  • 8
3
votes
3 answers

Deleting an object off the stack?

Just wondering since I am working with a partner on a C++ project, is it possible to explicitly delete an object that has been initialized on the stack? (so without pointer) For example: MinHeap h(data); // on stack Vs MinHeap *h = new…
Fox
  • 595
  • 1
  • 4
  • 10
3
votes
1 answer

Linked List Stack Loses Last Item Pushed on Stack

I am working on a Linked List implementation of a Stack, and seem to have what I need with only one error. I am inserting 3 strings, but before the 3rd string is popped, I get a NullPointerException. In running debug I found that this missing…
Chris
  • 934
  • 1
  • 17
  • 38
3
votes
1 answer

set length of `geom_hline` in `geom_bar` plot

I have an geom_bar plot and I would like to set the length of the geom_hline. I have this data, set.seed(666) df <- data.frame( date = seq(Sys.Date(), len= 156, by="4 day")[sample(156, 26)], IndoorOutdoor = rep(c(-1,1), 13), #Change so there…
Eric Fail
  • 8,191
  • 8
  • 72
  • 128
3
votes
2 answers

Minimum number of operation needed to sort a sequence of numbers in stack

We have N numbers in a stack and we want to sort them with minimum number of operations. The only available operation is reversing last K numbers in top of the stack (K can be between 2 and N). For example to sort sequence "2 3 4 5 1", we need 2…
Ali
  • 6,808
  • 3
  • 37
  • 47
3
votes
1 answer

Assembly, arithmetic operation on local variable on the stack

I've been playing around with inline assembly in Visual C++ lately and i was wondering if i could directly add a value to a local variable on the stack, for example: push 5 add [esp], 7 Is it okay to do this? I'm asking cause I've had some weird…
3
votes
2 answers

Assembly (MIPS) proper use: Registers vs. Stack

When writing MIPS programs, I've heard that its generally good practice to keep the registers clean i.e. to clear the values of the registers to 0 at the end of the program. So my first question is how/why is this necessary/good…
Tahsin M
  • 129
  • 2
  • 7
3
votes
1 answer

Decreasing of the stack pointer by creating local variables

To get a better understanding of binary files I've prepared a small c++ example and used gdb to disassemble and look for the machine code. The main() function calls the function func(): int func(void) { int a; int b; int c; int d; …
NouGHt
  • 31
  • 3
1 2 3
99
100