Skip to main content
34 votes

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

All of the above. It's complicated. Some CPU architectures, especially x86, include a dedicated stack pointer register and instructions that allow you to easily push/pop values on that stack. The ...
amon's user avatar
  • 135k
14 votes

In C++; How big should an object [that will be transferred between functions] be before I consider delegating it to the heap?

Since most implementations take the heap and the stack from the same block of memory (growing from either end) it doesn't matter. Size is not a reason to prefer the heap over the stack. The lifetime ...
candied_orange's user avatar
13 votes
Accepted

How can the size of a heap be less than its length?

In general, heaps will support insertion of items. When an element is inserted it is very inefficient to re-allocate the backing array and copy the entire heap just to fit one more element. So the ...
JonasH's user avatar
  • 5,724
10 votes

Different types of heap in java

While the other answers are giving good information about the internal details of certain various JVM heap designs, from the programmer's perspective, there is only one heap and one way to use it -- ...
Erik Eidt's user avatar
  • 34.2k
10 votes
Accepted

Why would you use 'new' and 'delete' for something that will be referenced through a vector?

There may be several reasons for proceeding this way, and in particular: Polymorphic container: the vector keeps objects of different classes sharing a same base class and uses polymorphism. A vector ...
Christophe's user avatar
  • 78.4k
9 votes

Is it a good idea to use strings in a struct as values to static properties?

I'll assess what you both said: For value types yes you gain memory/gc benefits. True. With strings they are ref types so allocate to the heap and won't give any benefit. True. ..I'm simply ...
Alexander's user avatar
  • 4,954
9 votes

In C++; How big should an object [that will be transferred between functions] be before I consider delegating it to the heap?

A million of characters are not to be passed by value, because passing by value (beyond a couple of words which could hold in registers) is done via the stack. And the stack space is always limited. ...
Christophe's user avatar
  • 78.4k
9 votes

Do all threads share the same instance of a heap variable, or have different instances of a heap variable?

There is no such thing like a "heap variable". You have invented a new term, so expecting others to explain you the threading behaviour of this new thing does not make much sense to me. ...
Doc Brown's user avatar
  • 209k
8 votes

how is stack and heap are assigned to each processes?

What you're fundamentally missing is that P1 and P2 each get their own stack. So the resulting pictures are much cleaner than you are imagining. You have two stacks, one for P1 and one for P2. They ...
Erik Eidt's user avatar
  • 34.2k
7 votes

Do all threads share the same instance of a heap variable, or have different instances of a heap variable?

Initially I cannot understand your question, because the phrase "heap variable" is not described with sufficient detail and clarity, such that its meaning is a bit ambiguous. Each call to ...
rwong's user avatar
  • 16.9k
7 votes
Accepted

Is O(log n) for memory management considered slow?

The "ideal" performance of an algorithm in people's minds is dependent on what the best option is out there. If you can do a "find" operation on a data structure in O(n log N) time, is that fast ...
Cort Ammon's user avatar
  • 11.6k
7 votes

In C++; How big should an object [that will be transferred between functions] be before I consider delegating it to the heap?

So the question basically is: “when should I use new?” Local variables always use stack space, but we can explicitly allocate objects on the heap with a new expression. When deciding for or against ...
amon's user avatar
  • 135k
7 votes
Accepted

Does Java copy method parameters to the stack frame of the called method?

In Java, the distinction stack vs. heap is not very meaningful, because Java doesn't give you a choice where values live. Conceptually, all objects live somewhere on the heap, but in Java an object is ...
amon's user avatar
  • 135k
7 votes

Different types of heap in java

tl;dr summary: I couldn't find much information online. Are there different types of heap memory available in Java ? Short answer: No. Slightly longer answer: The Java Language Specification does ...
Jörg W Mittag's user avatar
6 votes
Accepted

Different types of heap in java

Heap is divided Young Generation, Old or Tenured Generation, and Permanent Generation. The Young Generation is where all new objects are allocated and aged. When the young generation fills up, this ...
RK_Aus's user avatar
  • 241
5 votes

Is O(log n) for memory management considered slow?

"Slow" depends on the application. For some real-time applications (high speed machinery control, DSP, etc.), any non-bounded latency might be too slow, perhaps even leading to a catastrophic failure ...
hotpaw2's user avatar
  • 7,968
5 votes
Accepted

Should I always allocate QObject and derived classes to the heap?

The motivation for not putting QObjects on the stack is the Qt ownership model. If an object is destroyed all of its child objects are automatically deleted. If one of those child objects is allocated ...
Winston Ewert's user avatar
5 votes

What is time complexity of update in binary heap?

I'm going to disagree a bit with Scara95. With out context of your answer this seems like whether or not your answer is wrong could possibly be a matter of pedantry. Technically, yes, its not O(N) + ...
Krupip's user avatar
  • 1,300
5 votes

What is the standard for heap allocators in bare metal programs?

I can't say what the currently normal or "standard" practice is, but allocators were a big part of my master's thesis. That was over 20 years ago, but I think most of the basic facts are ...
Michael Borgwardt's user avatar
4 votes
Accepted

How is the Heap data structure useful?

A heap is always perfectly balanced. A Red Black tree has a bound on how unbalanced it can be, but it will still often be less than perfectly balanced. While this won't affect the Big O value of the ...
Servy's user avatar
  • 1,976
4 votes

How does Java and other managed languages achieve any performance, if everything is allocated at random places of the heap?

After thinking on it for a while, I have no idea how developers in banks, some HFT companies (!!!), stock markets and other businesses that require high performance, manage to write fast (not "...
candied_orange's user avatar
3 votes

Is it a good idea to use strings in a struct as values to static properties?

His argument: "I am saying there is no performance gain because there are strings inside of them. For value types yes you gain memory/gc benefits. With strings they are ref types so allocate to ...
Pharap's user avatar
  • 578
3 votes
Accepted

Could a language allow for persistent allocations without heap?

The critical question is how you define a heap, and how object lifetimes can be bounded. I understand a heap as a store of objects with dynamic lifetimes – once the lifetime of an object ends, its ...
amon's user avatar
  • 135k
2 votes

Why is it called a memory leak?

Just think about a programs memory usage like a closed water cooling system in a car: The water is pumped out of the reservoir to the parts that need cooling, then on to the radiator, and returned to ...
cmaster - reinstate monica's user avatar
2 votes

Why doesn't a max-heap have a decrease-key operation, and a min-heap a increase-key operation?

This have been discussed before here. To sum up, these operations can easily be implemented in O(log(n)) but usually aren't provided because they have little interest from a pure algorithmic ...
Diane M's user avatar
  • 2,076
2 votes
Accepted

Segmentation and Object File Segments

The segmented memory model of the 8086 (x86 real mode) is indeed something that belongs to the past. With 32 and 64 bits cpus, there's no longer a need for segment registers to extend 16 bit registers ...
Christophe's user avatar
  • 78.4k
2 votes

Heaps: Why is there a tradeoff between amount of space occupied (fragmentation), and speed at which operations are carried out?

But I was wondering how these two criteria were related, namely, why there is a 'tradeoff' and why a faster heap makes it difficult for a smaller heap in size. When a request comes in you have to ...
Peter Green's user avatar
  • 2,251

Only top scored, non community-wiki answers of a minimum length are eligible