Questions tagged [stack]

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

Filter by
Sorted by
Tagged with
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/...
Lazar Ljubenović's user avatar
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 ...
Jeff's user avatar
  • 129
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 ...
retrodev's user avatar
  • 387
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. ...
Ameer Abdallah's user avatar
-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/...
jsonphy's user avatar
  • 37
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 ...
Aviv Cohn's user avatar
  • 21.2k
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 ...
mydoghasworms's user avatar
-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 ...
Necromancer's 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; { ...
Anon's user avatar
  • 3,575
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 ...
Martin's user avatar
  • 3
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)) ...
Lance's user avatar
  • 2,561
-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 ...
geek's user avatar
  • 1
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 ...
Farrrbi's user avatar
  • 51
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 ...
Nergal's user avatar
  • 313
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 ...
Maksim Dmitriev's user avatar
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 ...
Govind Parmar's user avatar
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 ...
Gabriele Scarlatti's user avatar
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 ...
Sourabh Khandelwal's user avatar
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
Ravindu De Silva's 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 ...
Anonymus's 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 ...
jleach's user avatar
  • 2,642
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 ...
Anon's user avatar
  • 3,575
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 ...
max's user avatar
  • 1,075
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 ...
Tim's user avatar
  • 433
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 ...
Grad student's user avatar
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 ...
M. Evers's user avatar
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 ...
max's user avatar
  • 1,075
-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 ...
Liwizy's user avatar
  • 19
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 ...
Lynn's user avatar
  • 826
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 ...
navs4me's user avatar
  • 39
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) ...
Hugo's user avatar
  • 103
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 ...
Elliot Way's 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 ...
radscheit's user avatar
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. ...
Joseph Monroe's user avatar
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, ...
starflyer's 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, $...
kitty's 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....
moonman239's user avatar
  • 2,033
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 ...
moonman239's user avatar
  • 2,033
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 ...
Coder3000's 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 ...
Racheet's user avatar
  • 447
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?...
Joydip Ghosh's user avatar
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 ...
Carcigenicate's 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 }; ...
moonman239's user avatar
  • 2,033
-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 ...
Jean's user avatar
  • 101
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 ...
Moby Disk's user avatar
  • 230
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 ...
Nitkov's user avatar
  • 159
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) ...
tigrou's user avatar
  • 269
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++?" ...
mgokhanbakal's user avatar
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 ...
WZRh1jq91's 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 ...
Aviv Cohn's user avatar
  • 21.2k