Questions tagged [stack]

A LIFO (Last In, First Out) data structure.

A LIFO (Last In, First Out) data structure.

Adding an element to a stack is referred to as "pushing", removing an element is referred to as "popping".

See: Wikipedia: Stack

79 questions
102
votes
6 answers

Stack and Heap memory in Java

As I understand, in Java, stack memory holds primitives and method invocations and heap memory is used to store objects. Suppose I have a class class A { int a ; String b; //getters and setters } Where will the primitive a in…
Vinoth Kumar C M
  • 15,455
  • 23
  • 57
  • 86
56
votes
7 answers

Why does the call stack have a static maximum size?

Having worked with a few programming languages, I've always wondered why the thread stack has a predefined maximum size, instead of expanding automatically as required.  In comparison, certain very common high level structures (lists, maps, etc.)…
Lynn
  • 826
  • 1
  • 6
  • 8
55
votes
4 answers

How much stack usage is too much?

Lately when I've been writing C or C++, I'll declare all my variables on the stack just because it's an option, unlike with Java. However, I've heard that it's a bad idea to declare large things on the stack. Why exactly is this the case? I…
Elliot Way
  • 661
  • 1
  • 5
  • 5
41
votes
1 answer

Frame Pointer Explanation

In MIPS assembly, there is a register for the stack pointer, and another register for the frame pointer. What is the frame pointer and what is its purpose? How does it differ from the stack pointer?
41
votes
3 answers

Why does the stack grow downward?

I'm assuming there's a history to it, but why does the stack grow downward? It seems to me like buffer overflows would be a lot harder to exploit if the stack grew upward...
user541686
  • 8,074
  • 8
  • 38
  • 49
36
votes
6 answers

What's the point of implementing a Stack using two queues?

I have the following homework question: Implement the stack methods push(x) and pop() using two queues. This seems odd to me because: A Stack is a (LIFO) queue I don't see why you would need two queues to implement it I searched…
Carcigenicate
  • 2,634
  • 3
  • 24
  • 38
33
votes
6 answers

Why do programs use call stacks, if nested function calls can be inlined?

Why not have the compiler take a program like this: function a(b) { return b^2 }; function c(b) { return a(b) + 5 }; and convert it into a program like this: function c(b) { return b^2 + 5 }; thereby eliminating the computer's need to remember…
moonman239
  • 2,023
  • 4
  • 18
  • 23
29
votes
6 answers

Why do we need a Heap if everything can be done much more efficiently on the Stack?

This is actually somewhat related to the question I asked yesterday about why both a Stack and a Heap are necessary in the applications we use today (and why we can't just go with a Heap instead of both, in order to have a simple & singular standard…
Dark Templar
  • 6,223
  • 16
  • 46
  • 46
26
votes
4 answers

Understanding stack frame of function call in C/C++?

I am trying to understand how stack frames are built and which variables (params) are pushed to stack in what order? Some search results showed that the C/C++ compiler decides based on operations performed within a function. For example, if the…
Gana
  • 401
  • 1
  • 5
  • 6
24
votes
2 answers

How are the size of the stack and heap limited by the OS?

Note: if you need to consider a specific OS to be able to answer, please consider Linux. Whenever I run a program, it will be given a virtual memory space to run in, with an area for its stack and one for its heap. Question 1: do the stack and the…
Daniel Scocco
  • 5,832
  • 9
  • 39
  • 47
22
votes
4 answers

Why is putting something on the stack called "push"?

According to http://dictionary.reference.com push verb (used with object) to press upon or against (a thing) with force in order to move it away. to move (something) in a specified way by exerting force; shove; drive: to push something aside; to…
doc
  • 223
  • 2
  • 4
19
votes
3 answers

Are the Stack and Heap hardware, OS, or language-specific concepts?

In languages such as C or Java we have the concepts of the Stack and the Heap. Are these abstractions the particular language runtime/compiler creates over the plain-old RAM? Or are these concepts inherent to the OS? In other words, do the Stack and…
Aviv Cohn
  • 21,190
  • 31
  • 118
  • 178
18
votes
1 answer

What is the purpose of red zone?

Red zone is a fixed size area in memory beyond the stack pointer that has not been "allocated". Compilers do generate assembly to access that area in simple leaf functions. But I can't see any real advantages for red zone. Accessing memory beyond…
15
votes
4 answers

When there's no TCO, when to worry about blowing the stack?

Every single time there's a discussion about a new programming language targetting the JVM, there are inevitably people saying things like: "The JVM doesn't support tail-call optimization, so I predict lots of exploding stacks" There are thousands…
Cedric Martin
  • 1,067
  • 10
  • 16
14
votes
2 answers

Amortized Analysis? (Worst-case Performance Guarantees)

What is Amortized Analysis? And how can it help me achieve worst-case performance guarantees in my programs? I was reading that the following techniques can help the programmer achieve Worst-case performance guarantees (i.e. in my own words:…
Anthony
  • 900
  • 11
  • 17
1
2 3 4 5 6