Questions tagged [stack]

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

Filter by
Sorted by
Tagged with
1 vote
4 answers
247 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 ...
user avatar
  • 129
4 votes
4 answers
211 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 ...
user avatar
  • 377
0 votes
1 answer
111 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. ...
user avatar
-4 votes
4 answers
647 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/...
user avatar
  • 37
14 votes
3 answers
2k 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 ...
user avatar
  • 20.7k
0 votes
3 answers
212 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 ...
user avatar
-1 votes
4 answers
4k 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 ...
user avatar
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; { ...
user avatar
  • 3,319
0 votes
1 answer
2k 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 ...
user avatar
  • 3
0 votes
2 answers
214 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)) ...
user avatar
  • 2,406
-3 votes
2 answers
8k 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 ...
user avatar
  • 1
3 votes
1 answer
722 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 ...
user avatar
  • 41
2 votes
3 answers
1k 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 ...
user avatar
  • 313
2 votes
2 answers
3k 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 ...
user avatar
3 votes
2 answers
347 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 ...
user avatar
3 votes
1 answer
275 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 ...
user avatar
3 votes
1 answer
851 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 ...
user avatar
0 votes
2 answers
144 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
user avatar
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 ...
user avatar
  • 211
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 ...
user avatar
  • 2,642
8 votes
6 answers
3k 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 ...
user avatar
  • 3,319
1 vote
3 answers
964 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 ...
user avatar
  • 1,045
13 votes
6 answers
4k 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 ...
user avatar
  • 423
2 votes
2 answers
918 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 ...
user avatar
0 votes
4 answers
945 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 ...
user avatar
5 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 ...
user avatar
  • 1,045
-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 ...
user avatar
  • 19
54 votes
7 answers
15k 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 ...
user avatar
  • 806
1 vote
1 answer
11k 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 ...
user avatar
  • 19
0 votes
3 answers
619 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) ...
user avatar
  • 103
50 votes
4 answers
55k 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 ...
user avatar
-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 ...
user avatar
4 votes
2 answers
10k 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. ...
user avatar
1 vote
1 answer
290 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, ...
user avatar
  • 119
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, $...
user avatar
  • 49
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....
user avatar
  • 2,021
12 votes
9 answers
14k 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 ...
user avatar
  • 2,021
0 votes
1 answer
891 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 ...
user avatar
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 ...
user avatar
  • 447
5 votes
2 answers
19k 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?...
user avatar
36 votes
6 answers
13k 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 ...
user avatar
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 }; ...
user avatar
  • 2,021
-1 votes
1 answer
541 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 ...
user avatar
  • 101
3 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 ...
user avatar
  • 220
3 votes
2 answers
309 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 ...
user avatar
  • 159
6 votes
4 answers
680 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) ...
user avatar
  • 259
0 votes
3 answers
356 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++?" ...
user avatar
3 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 ...
user avatar
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 ...
user avatar
  • 20.7k
0 votes
1 answer
163 views

confusion regarding handling of new smart pointers on stack frames?

Let me try to elaborate it. Stack Frame: When we execute any function it create stack where all local variables and instructions reside. And Smart Pointer: smart pointer like std::unique_ptr allows ...
user avatar
  • 101