-3

I am trying to code a relatively simple program to understand in which scenarios it would be more efficient and useful to use the heap.

  1. I first read that you better store large objects on the heap. So I created a std::vector on the heap and filled it with an insane amount of bytes, something like 18Gb. At some point, it threw a std::bad_alloc exception, then my OS (Linux mint) killed the process after the swap was full. But the result was the same with the stack, so I don't understand how it's better on the heap.

  2. Maybe I lack creativity, but I cannot think of a design where I would absolutely need to use the heap, I can always pass a reference of my object on the stack and achieve the same memory usage efficiency. For every program I wrote, it was always more efficient to use the stack speed wise and was the same for memory usage.

So, in what scenario is it useful to use the heap regarding memory usage efficiency or speed?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 3
    Stack is like 8MB tops. `std::vector` allocates on the heap internally, perhaps that's what is confusing you? – Yksisarvinen Dec 09 '22 at 18:46
  • yeah that makes sense because i was looking at the stack space and it was about 8mb... i just thought it was getting reallocated or something – Patrick Charron Morneau Dec 09 '22 at 18:48
  • 1
    "But the result was the same with the stack" it certainly was not the same. Stack memory is much more limited. If you have same amount of stack as heap memory, a rather strange system that would be – 463035818_is_not_an_ai Dec 09 '22 at 18:50
  • 1
    The main reason you would use the heap is that you don't know how long or how big your allocation is. The heap has the most flexibility. However notice that std::vector is storing its data on the heap already! and that is why you might use the heap directly a lot less in C++ than in C. – user253751 Dec 09 '22 at 18:51
  • well the result was the same because as Yksisarvinen mentioned std::vector are allocated on the heap or store element on the heap – Patrick Charron Morneau Dec 09 '22 at 18:52
  • try to comare `std::array` vs `std::vector` – 463035818_is_not_an_ai Dec 09 '22 at 18:52
  • I think, the most important reason why the heap is used are objects (or data in general) with a size that is determined at runtime. Everything else can be broken down to this. But understanding all the situations needs many more words. First you must understand how the stack works and why the stack does not do what you want in certain situations. So a good answer must be very long. – habrewning Dec 09 '22 at 20:04
  • See the answer here: https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new/6500497#6500497 – habrewning Dec 09 '22 at 21:03

1 Answers1

2

You use the heap whenever you need more flexibility than the stack provides. If you need to allocate memory to return something from a function, then it can't be on the stack, because it would be freed when the function returned. And it can't be global, because you might call the same function more than once.

In C++, you might not always realize that you are using the heap, because classes like std::vector and std::string use the heap internally. You don't always have to write new yourself to use the heap.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
user253751
  • 57,427
  • 7
  • 48
  • 90
  • That answer does not help a lot. Of course you can allocate memory and return something from a function. Pointers are made for this purpose. You only have to think, where on the stack or in the global memory you want to store your data. – habrewning Dec 09 '22 at 20:13
  • @habrewning how do you allocate memory on the stack and then return a pointer to it from a function and make that be useful to whoever called your function? – user253751 Dec 09 '22 at 20:15
  • You can use global memory and pass a pointer into it back. You can create a pointer outside the function and pass it to the function. You can have a storage pool. If for you "allocate" and the "new" means the same, then it does not work. But "allocate" is a normal English word that can be used in many contexts. Normally you have some functional requirements to implement. Whether you use the stack or the heap is not of interest for your customer. Whether you use the heap and the new operator for allocating things or whatever mechanism does not matter. – habrewning Dec 09 '22 at 20:30
  • Are you sure that when you do `return MyClass();` that the result is not created in the stack frame of the caller? I don't know the answer. But perhaps even this works to return something that you have created inside the function. On the other hand it is true, that you cannot return an array, so probably without your own memory mechanisms you must use the heap. I think, I have got your point. – habrewning Dec 09 '22 at 20:39
  • @habrewning you can return values with known sizes on the stack, because the caller allocates space to store them. You cannot use global memory if you call the function more than once at the same time. – user253751 Dec 09 '22 at 20:47
  • If for some reason you cannot use the heap, you can find a solution for counting calls or such things. It is not that you are lost when not having heap memory. – habrewning Dec 09 '22 at 20:51
  • @habrewning yes, you could write your own heap memory allocator :) – user253751 Dec 09 '22 at 20:53
  • And that was the question. What do you lose when not using the heap? And your example was one that with a bit of phantasy you can be implemented without the heap. Does this work always? – habrewning Dec 09 '22 at 20:54