Questions tagged [stack]

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

1
vote
1answer
131 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
3answers
74 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
vote
2answers
110 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 ...
0
votes
2answers
136 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
1answer
418 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
2answers
200 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
2answers
239 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 ...
3
votes
1answer
410 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
3answers
482 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
2answers
2k 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
2answers
255 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
1answer
198 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
1answer
575 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
2answers
111 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
2answers
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 ...
10
votes
2answers
865 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 ...
7
votes
6answers
1k 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
3answers
722 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 ...
11
votes
5answers
2k 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 ...
1
vote
2answers
596 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 ...
0
votes
4answers
641 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 ...
2
votes
2answers
888 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
1answer
1k 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 ...
43
votes
7answers
9k 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 ...
0
votes
1answer
7k 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
3answers
129 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) ...
13
votes
4answers
13k 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 ...
-2
votes
1answer
1k 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
2answers
9k 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
1answer
249 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
2answers
1k 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, $...
4
votes
3answers
4k 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....
10
votes
9answers
8k 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
1answer
716 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 ...
5
votes
1answer
3k 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 ...
2
votes
2answers
12k 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?...
34
votes
6answers
12k 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 ...
32
votes
6answers
8k 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
1answer
478 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 ...
2
votes
1answer
2k 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
2answers
302 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
4answers
594 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
3answers
283 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++?" ...
3
votes
3answers
2k 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
2answers
1k 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 ...
0
votes
1answer
113 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 ...
2
votes
1answer
298 views

How to efficiently sort a recursively defined Stack?

I am trying to implement a recursively defined Stack and sort it in Java. I don't have a particular usage of this program in mind. I found this approach of stack implementation a bit useful while ...
0
votes
2answers
148 views

Should you avoid adding unecessary stack frames?

I have been working with laravel (PHP) recently, and there is an option when using their fluent ORM to define where clauses using closures. This gives us two, functionally equivalent, ways to specify ...
12
votes
1answer
5k views

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 ...
13
votes
4answers
2k views

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 ...