Questions tagged [stack]
A LIFO (Last In, First Out) data structure.
79
questions
3
votes
4
answers
389
views
Iterating through stacks and queues?
Are stacks and queues allowed to be iterated over, conceptually?
It “feels wrong” for some reason but I can't come up with a reason why it would be bad to see through the whole thing that's stacked/...
1
vote
4
answers
503
views
Could a language allow for persistent allocations without heap?
Is it possible in theory to have a programming language that allows for object lifetimes that exist beyond the current scope but without using heap?
As a little background, in embedded development it ...
5
votes
4
answers
323
views
Use of globals in stack-based virtual machine implementation
I'm implementing a stack-based virtual machine in C.
The following variables are used by pretty much every function:
memory array
various pointers to memory offsets
program counter
stack
stack ...
0
votes
1
answer
134
views
Which scope should markers for a Stack Allocator fall under?
For reference, I am reading from "Game Engine Architecture 2nd Edition" by Jason Gregory.
Although I understand the theory behind Stack Allocators, I am having trouble implementing it fully. ...
-4
votes
4
answers
1k
views
C# Why should i limit myself to List or Stack ? ( instead of having both)
List is implemented in C# exactly as Stack, see:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.stack-1.push?view=netframework-4.8#remarks
https://docs.microsoft.com/en-us/...
19
votes
3
answers
3k
views
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 ...
0
votes
3
answers
240
views
Resolving function calls as function arguments using a stack
After doing a bit of reading, I have a vague understanding of the use of a stack in calling functions when one function calls another, where the arguments are placed on the stack for the called ...
-1
votes
4
answers
6k
views
Is it a good idea to use strings in a struct as values to static properties?
I'm in a discussion with a co-worker concerning the use of structs. I have a couple of structs that contain several static properties that are used throughout our website. The value of those ...
1
vote
2
answers
1k
views
Should I always allocate QObject and derived classes to the heap?
I was in #Qt irc channel, and I showed a small snippet of my code in a style that I heavily rely upon. It looks like this:
/* Get Reply from Server */
QPointer<QNetworkReply> reply;
{
...
0
votes
1
answer
3k
views
Registers and Stacks in NASM
So, I am more or less voluntarily learning NASM, and I have problems finding sources that really explain it. Unlike with Java or C# I can't just use google as well, since Assembly just isn't used by ...
0
votes
2
answers
218
views
How to Implement a `function` with `return` Without Using the `function` keyword
Along the lines of How to Simulate Control-Flow without using Control-Flow Primitives, I am wondering how to simulate return from a function.
Given an example setup like this:
console.log(a(10))
...
-3
votes
2
answers
10k
views
Why we use top== -1 for implementation of stack using simple array?
I am a newbie at data structures.
I have read an implementation of a stack using a simple array. The algorithm for this implementation is presented below.
Stack implementation in terms of an array
...
4
votes
1
answer
824
views
C++ Are members of a class pointer automatically on heap?
Let's say we have a struct Vector2i { int x = 0, int y = 0 };
And create a Pointer to it via Vector2i* pointer = new Vector2i;
Where would int x and int y be stored? Heap or stack?
Are all members ...
2
votes
3
answers
2k
views
How do Stack Machine store global vars?
How exactly do stack machines (both real and virtual stack machines) store global variables?
I know that C(++) just compile it to the .data segment of a program's memory segmentation.
Then there's ...
2
votes
2
answers
4k
views
Does Java copy method parameters to the stack frame of the called method?
This is part of the answer about stack and heap in Java:
So, why have the stack or the heap at all? For things that leave
scope, the stack can be expensive. Consider the code:
void foo(String ...
3
votes
2
answers
372
views
When designing an ISA , is it a good design decision to permit a `pop` instruction that does not require an operand to receive the popped value?
A lot of ISAs including x86, x64, ARM, Itanium, have a pop instruction that requires an operand which is usually a register (or, in ARM's case, a register list). In fact, I can't think of an ISA off ...
3
votes
1
answer
362
views
Segmentation and Object File Segments
I'm struggling to really understand memory segmentation.
It's the topic of memory segmentation related to segments like heap,stack,code in an object file?
Maybe they're for sure, but I still ...
3
votes
1
answer
906
views
Why doesn't greedy approach work for following problem but the solution provided in the editorial does?
Problem Statement:
Alexa has two stacks of non—negative integers, stack A = [a0,a1, . . . ,an_1]
and stack
B = [b0, b1, . . . ,bm_1] where index 0 denotes the top of the stack. Alexa challenges Nick ...
0
votes
2
answers
171
views
Implementing additional function to stack data structure
How would i design a stack which in addition to push() and pop() ,also has a function min which returns the minimums element ? min() must operate in big O(1) time
10
votes
2
answers
2k
views
Limit of the stack
Recently I've tested the limit of a stack on three devices with different OSs (by limit, I mean the maximum number of levels that can the stack have), and I noticed that every time when I hit 2^16 ...
12
votes
2
answers
2k
views
Is a stack structure used for async processes?
This question has an excellent answer by Eric Lippert describing what the stack is used for. For year's I've known - generally speaking - what the stack is and how it's used, but parts of his answers ...
8
votes
6
answers
4k
views
In C++; How big should an object [that will be transferred between functions] be before I consider delegating it to the heap?
In my day to day programming, I tend to use very few pointers, not only because I want to keep my code simple and error free, but because I assume that the programming that I do does not have any ...
1
vote
3
answers
1k
views
Why is call stack not implemented as a dynamic array in modern OS? [duplicate]
When a modern OS loads a process, it pre-allocates a certain amount of space for stack. This means that the programmer has to be careful to avoid stack overflow by limiting call depth and/or by ...
13
votes
6
answers
5k
views
Is there a drawback at allocating a huge amount of the stack for a single array in an embedded system?
I usually have no problem deciding whether some data has to be global, static or on the stack (No dynamic allocation here, so no use of the heap). I have also read a few Q/A such as this one but my ...
2
votes
2
answers
1k
views
Stack implementation by Design by Contract vs Defensive Programming
I am trying to write Stack code using the two techniques i.e Design by Contract vs Defensive Programming but I am not sure if I am doing right or not.I am not throwing any kind of exception or error ...
1
vote
4
answers
1k
views
Is "Pushing a variable to the Stack" a misnomer in C++?
In C++ a variable is allocated in the 'Call Stack' ('Stack') or the 'Heap.' Frequently when referring to allocating a variable to the 'Stack', people use the phrase "Push to the Stack."
My issue with ...
7
votes
2
answers
3k
views
Proper usage of up / down the stack
What is the convention about describing the direction of the stack, i.e., about what the words up, down, top, and bottom mean?
I noticed that with stack data structure API, top usually to refer to ...
-1
votes
1
answer
2k
views
Big-Oh notation of a stackarray
I am studying data structures and I have hit a bit of a road block. The top is Big oh notation and it is simply confusing. While I can find the upper bound for the simplest of loops, when it comes to ...
56
votes
7
answers
16k
views
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 ...
3
votes
1
answer
12k
views
how is stack and heap are assigned to each processes?
How multiples processes are stored in the main memory , i understand every process will be divided into the equal size pages and will be stored in the frames of main memory. if whole main memory is ...
0
votes
3
answers
817
views
Value addresses of functions
I'm reading Yung-Hsiang Lu's book, Intermediate C Programming, and I'm working through the chapter on stack memory. When defining the value address he provides this code example:
int f1(int k, int m) ...
55
votes
4
answers
66k
views
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 ...
-3
votes
1
answer
2k
views
stack based vs heap based overflow [closed]
Why is it more difficult to perform a heap based buffer overflow than a stack based? (regarding x86 architecture)
I thought it could be the fact that heaps are allocating memory dynamically. But is ...
4
votes
2
answers
11k
views
Can a tree be used to create a stack?
I'm aware that linked lists, sets and arrays can be used to create stacks by themselves. The theory behind it is this
linked-list: In some languages, a linked-list is substitutable for an array. ...
1
vote
1
answer
296
views
Is stack allocation in Java implementation dependent?
I've looked through the different threads, and there are a lot of conflicting information out there. The most useful article I found was this: Java theory and practice: Urban performance legends, ...
0
votes
2
answers
2k
views
Undo/redo implementation for file changes in PHP
I was thinking to store all file operations inside an array, along with the reverse operations which are used for undos.
Example:
[
[
'op' => 'move',
'parameters' => [$path_from, $...
6
votes
3
answers
7k
views
In x86, where are the memory addresses of the values on the stack located?
Suppose I have a very simple C program that just does this:
int i = 6;
int j = 4;
int k = 5;
int a = i + j + k;
Since i, j, and k are on the stack, they will be located relative to the stack pointer....
12
votes
9
answers
15k
views
I understand what a stack pointer is - but what is it used for?
The stack pointer points to the top of the stack, which stores data on what we call a "LIFO" basis. To steal someone else's analogy, it's like a stack of dishes in which you put and take dishes at the ...
0
votes
1
answer
927
views
Implement Stack for Toy VM C++
Recently for fun I have decided to build a toy programming, compiler and a low-level register based interpreted vm. While starting to implement the virtual machine I got stuck. The stack which holds ...
8
votes
1
answer
6k
views
Why is a frame pointer set as an offset from the stack pointer? [duplicate]
I'm trying to understand how stack frames are constructed and have run into this description on wikipedia:
The locations of all other fields in the frame can be defined relative
either to the top ...
5
votes
2
answers
20k
views
why use postfix /prefix expression instead of infix?
I understand how to convert infix to postfix/prefix but I do not understand why postfix or prefix expression are used in computer system?
What is the advantage of postfix prefix over infix expression?...
36
votes
6
answers
14k
views
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 ...
33
votes
6
answers
9k
views
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 };
...
-1
votes
1
answer
563
views
How implement an IP stack? [closed]
I want to understand what implement an IP stack means.
I explain myself : I've wrote two Java little applications (Client-Server) which communicate over a LAN with TLS. In my code, I didn't have to ...
4
votes
1
answer
3k
views
How much physical memory is consumed by the stack of a .NET thread?
In Stephen Cleary's article in MSDN magazine "Introduction to Async/Await on ASP.NET" he says that "every thread pool thread on a modern OS has a 1MB stack." (modern OS == Windows 7/8 for this ...
3
votes
2
answers
311
views
Place variables frequently used by the same function on the heap?
Say that I have the following situation:
void myFunc()
{
int x;
//Do something with x
}
"x" is placed on the stack which is no doubt fast.
Now, "myFunc" is called very frequently, lets say ...
6
votes
4
answers
743
views
How to convert the following node evaluation procedure to a non recursive solution?
I have the following recursive method.
It evaluates a node (that represent a logical expression), using deep first search traversal :
EvaluateNode(Node node)
{
bool result;
switch(node.Type)
...
0
votes
3
answers
371
views
Objects in smalltalk and c++ [closed]
I have a question based on SmallTalk and C++. In one of the programming languages books, there is a question like:
"Does an object in Smalltalk require its own private stack? What about in C++?"
...
4
votes
3
answers
3k
views
instance methods, stack or heap
Do methods (and their variables) that belong to an object instance go on the stack or the heap?
Ex
Main()
{
Myclass Myobj = new Myclass();
Myobj.Doit();
}
class Myclass
{
Void Doit()
{
Int myint ...
2
votes
2
answers
2k
views
How does the CPU access the values of stack-allocated variables? [duplicate]
Consider the following C++ function:
void doStuff() {
Thing thingA;
Thing thingB;
thingA.doSomething();
// .. etc
}
During the execution of this function, variables thingA and thingB ...