Questions tagged [heap]

55 questions
102
votes
6 answers

Stack and Heap memory in Java

As I understand, in Java, stack memory holds primitives and method invocations and heap memory is used to store objects. Suppose I have a class class A { int a ; String b; //getters and setters } Where will the primitive a in…
Vinoth Kumar C M
  • 15,455
  • 23
  • 57
  • 86
29
votes
6 answers

Why do we need a Heap if everything can be done much more efficiently on the Stack?

This is actually somewhat related to the question I asked yesterday about why both a Stack and a Heap are necessary in the applications we use today (and why we can't just go with a Heap instead of both, in order to have a simple & singular standard…
Dark Templar
  • 6,223
  • 16
  • 46
  • 46
24
votes
2 answers

How are the size of the stack and heap limited by the OS?

Note: if you need to consider a specific OS to be able to answer, please consider Linux. Whenever I run a program, it will be given a virtual memory space to run in, with an area for its stack and one for its heap. Question 1: do the stack and the…
Daniel Scocco
  • 5,832
  • 9
  • 39
  • 47
19
votes
3 answers

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 inherent to the OS? In other words, do the Stack and…
Aviv Cohn
  • 21,190
  • 31
  • 118
  • 178
15
votes
5 answers

Java Heap Allocation Faster than C++

I already posted this question on SO and it did ok. It was unfortunately closed though(only needs one vote to reopen) but someone suggested I post it on here as it is a better fit so the following is literally a copy paste of the question I was…
aaronman
  • 685
  • 1
  • 6
  • 13
10
votes
1 answer

Why is the main memory for object allocation called the 'heap'?

Has anybody got an idea why the area of main memory where objects are allocated is referred to as the heap. I can understand the rationale for that of the stack LIFO but would like to know what the rationale is for the 'heap' name.
cobie
  • 3,237
  • 5
  • 23
  • 21
9
votes
2 answers

What is time complexity of update in binary heap?

For a binary heap we have O(log(n)) for insert, O(log(n)) for delete min and heap construction can be done in O(n). In the context of using a binary heap in Djikstra, my exam paper involved an "update" in the heap where the priority of a vertex is…
Brendan Hill
  • 251
  • 2
  • 6
9
votes
3 answers

A good C Variable Length Array example

This question got rather a freezing reception at SO, so I decided to delete it there and try here instead. If you think it does not fit here either, please at least leave a comment on suggestion how to find an example I'm after... Can you give an…
hyde
  • 3,744
  • 4
  • 25
  • 35
9
votes
3 answers

Custom heap allocators

Most programs can be quite casual about heap allocation, even to the extent that functional programming languages prefer to allocate new objects than modify old ones, and let the garbage collector worry about freeing things. In embedded programming,…
rwallace
  • 1,198
  • 11
  • 19
8
votes
6 answers

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 objects large enough to benefit from delegating them…
Anon
  • 3,565
  • 3
  • 27
  • 45
6
votes
2 answers

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

I'm going through some code from this article about ECS-systems in game programming and trying to understand it, and something I'm seeing a lot is using heap memory in places where it seems like there is no benefit in doing so. Take this as an…
JensB
  • 237
  • 1
  • 6
6
votes
4 answers

Stack and heap - dynamic allocation question

Sources usually mention that dynamically created variables are allocated on the heap, while functions' variables on the stack. Also the ones on the stack cease to exist automatically when e.g. the function which contains the variable exits. If I…
John V
  • 4,898
  • 10
  • 47
  • 73
5
votes
2 answers

MaxHeap-like data structure supporting max AND min, also decrease-key (yes decrease-key)

For the purpose of implementing an optimization algorithm (finding the minimum of a multivariate function) I want to create a data structure that supports the following operations: load from array Peek at maximum element (but don't destroy it) Peek…
Snowbody
  • 261
  • 1
  • 8
5
votes
3 answers

To find the oldest/newest element in a heap

I want to find the oldest/newest element added into a heap of size k. At any given point of time if I have to find say the oldest element in the heap is there any approach with O(1) space and O(1) time. I was thinking of an approach of having a…
redDragon
  • 95
  • 1
  • 7
5
votes
4 answers

Why is it called a memory leak?

I am a hobbyist programmer, bit of a stickler for terminology, currently learning C and recently came across the concept of Memory Leak. Now, I do understand what it means. Dynamic memory allocated to a program is not returned by the program. But…
user1720897
  • 599
  • 1
  • 5
  • 12
1
2 3 4