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

Browser Javascript Stack size limit

I am getting some client-side Javascript stack overflow issues specifically in IE browser, this is happening inside a third party library that makes some function calls and for some reason they occasionally brake in IE only due to it's low stack…
guilhebl
  • 8,330
  • 10
  • 47
  • 66
87
votes
2 answers

What is the default stack size, can it grow, how does it work with garbage collection?

I understand that each thread has its own stack. Primitive types and references are kept on the stack, and that no object is kept on the stack. My questions are: How much can a stack grow? (like with the paramters -Xms and -Xmx) Can we limit its…
codingenious
  • 8,385
  • 12
  • 60
  • 90
82
votes
12 answers

Calling delete on variable allocated on the stack

Ignoring programming style and design, is it "safe" to call delete on a variable allocated on the stack? For example: int nAmount; delete &nAmount; or class sample { public: sample(); ~sample() { delete &nAmount;} int nAmount; }
unistudent
  • 889
  • 1
  • 6
  • 5
82
votes
11 answers

Android remove Activity from back stack

Okay so I'm kind of stumped on what to do with this. So I have the MainActivity, and from there an Activity can be launched to DegreePlanActivity, and from there another Activity can be launched to EditDegreePlan. I've got EditDegreePlan set to…
Emrys90
  • 1,007
  • 1
  • 10
  • 15
79
votes
6 answers

Where can I find default -Xss (stack size) value for Oracle JVM?

Has anyone ever found a single JVM document listing default -Xss values for various versions of the Oracle JVM, and for different OS's? I've been able to find this table in the jrockit docs, but that's not helpful for those using the "normal" Oracle…
charlie arehart
  • 6,590
  • 3
  • 27
  • 25
78
votes
9 answers

Global memory management in C++ in stack or heap?

If I declare a data structure globally in a C++ application , does it consume stack memory or heap memory ? For eg struct AAA { .../.../. ../../.. }arr[59652323];
sameer karjatkar
  • 2,017
  • 4
  • 23
  • 43
76
votes
1 answer

View activity stack in Android

Is it possible to view the activity stack in Android for debugging purposes?
hpique
  • 119,096
  • 131
  • 338
  • 476
76
votes
2 answers

How does a NOP sled work?

I can't find a good source that answers this question. I know that a nop sled is a technique used to circumvent stack randomization in a buffer overflow attack, but I can't get my head around how it works. What's a simple example that illustrates…
amorimluc
  • 1,661
  • 5
  • 22
  • 32
73
votes
4 answers

Why is memory allocation on heap MUCH slower than on stack?

I have been told this many times. But I don't know WHY...What extra cost is involved when allocating memory from heap? Is it hardware related? Is it related to CPU cycles? So many guesses but no exact answers...Could someone give me some…
smwikipedia
  • 61,609
  • 92
  • 309
  • 482
71
votes
6 answers

How to get a stack trace object in Ruby?

I need to get a stack trace object in Ruby; not to print it, just to get it to do some recording and dumping for later analysis. Is that possible? How?
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
71
votes
2 answers

When and how to use GCC's stack protection feature?

I have enabled the -Wstack-protector warning when compiling the project I'm working on (a commercial multi-platform C++ game engine, compiling on Mac OS X 10.6 with GCC 4.2). This flag warns about functions that will not be protected against stack…
Guillaume
  • 4,901
  • 5
  • 24
  • 19
67
votes
2 answers

Array-Based vs List-Based Stacks and Queues

I'm trying to compare the growth rates (both run-time and space) for stack and queue operations when implemented as both arrays and as linked lists. So far I've only been able to find average case run-times for queue pop()s, but nothing that…
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
66
votes
2 answers

Does std::array<> guarantee allocation on the stack only?

Is std::array (without myself using new) guaranteed to be allocated in the stack rather then the heap by the C++-Standard? To be clear, I do not mean new std::array. I mainly wonder, if the standard library is allowed to use new…
towi
  • 21,587
  • 28
  • 106
  • 187
65
votes
4 answers

Android Application Class Lifecycle

The android app I am working on overrides the Application class to store lightweight state (username, gps location, etc) in static vars. Most of this state is set in OnCreate of the launch activity (username retrieved from prefs, location listener…
Patrick Cullen
  • 8,654
  • 3
  • 21
  • 23
64
votes
8 answers

How does a stackless language work?

I've heard of stackless languages. However I don't have any idea how such a language would be implemented. Can someone explain?
rlbond
  • 65,341
  • 56
  • 178
  • 228