2

for example:

void a()
{
    int bla;

    bla = 1;
}

vs.

void b()
{
    std::unique_ptr<int> bla( new int );

    *bla   = 1;
}

When is one or the other considered good practice? When isn't it? Or does it simply lie in the eye of the beholder? Does it only make sense when working on a large scale project or when working with large values?

Of course the heap is slightly slower.

deW1
  • 5,562
  • 10
  • 38
  • 54
  • 1
    Consider that the stack size on Windows by default is 1 Megabyte, if you plan to use more you should definitely go for the heap instead of changing the defaults – Marco A. Sep 07 '14 at 17:23
  • @KugBuBu: No. This a question asking when it's appropriate to use automatic storage (e.g. stack) and allocated storage (e.g. heap). – someguy Sep 07 '14 at 17:29
  • 2
    You shouldn't be using the second code snippet, ever. If you want to use heap allocation, do it with `std::unique_ptr` or `std::unique_ptr` or `std::vector`. – Ben Voigt Sep 07 '14 at 17:31
  • @someguy I think I got the wrong link. There's already question that covers everything in "heap vs stack" thing. – KugBuBu Sep 07 '14 at 17:31
  • 1
    @KugBuBu: You can't use the duplicate suggestion tool twice, but you can leave another comment with a better duplicate. – Ben Voigt Sep 07 '14 at 17:32
  • I'm sure a question like this has been answered. It's something you're likely to ask when first introduced to the two concepts without prior programming experience. – someguy Sep 07 '14 at 17:34
  • Use the stack when you can, the heap when you have to - e.g. if the object's lifetime needs to end at a different time. – Alan Stokes Sep 07 '14 at 17:34
  • @BenVoigt I am worried because I can't see the related section. I see just linked and nothing else. wut? – KugBuBu Sep 07 '14 at 17:36
  • @KugBuBu: Same here, maybe they're making changes to the related algorithm. If it doesn't fix itself soon we should ask on Meta. – Ben Voigt Sep 07 '14 at 17:44
  • @KugBuBu same here maybe that's why I couldn't find the question you were talking about. – deW1 Sep 07 '14 at 17:50
  • @BenVoigt Stackoverflow's search engine is broken since forever. (But now it's more broken) – KugBuBu Sep 07 '14 at 17:58
  • 2
    @deW1 Anyway I found something that might help you: http://stackoverflow.com/questions/7973138/how-do-i-choose-heap-allocation-vs-stack-allocation-in-c – KugBuBu Sep 07 '14 at 17:59
  • @KugBuBu: And that would be the correct duplicate :) – Ben Voigt Sep 07 '14 at 18:03
  • @BenVoigt I changed the second example function :) thank you as well as KugBuBu – deW1 Sep 07 '14 at 18:06

3 Answers3

3

On a Windows platform the default stack limit is ~1 megabyte, that means you should definitely put bigger objects on the heap instead of changing the default values (or worse doing it anyway and hope for the best). Check your environment stack size limit before experimenting with it. Also: if your algorithm is a recursive one bear in mind that your stack limit will also put under pressure. Thus also pay attention to your algorithm.

One important point to bear in mind is that stack objects will be destroyed at the end of the function call while heap ones (unless you're using smart pointers - which is recommended) will not. You should plan your choice accordingly. As a rule of thumb big long-time-spanning objects should go on the heap, with some exceptions.

For most applications the performance differences are kinda negligible too. Don't even think of structuring your whole program because of the small performance gain with stack allocations, premature optimization is the root of all evils. Furthermore huge slowdowns usually come from excessive copying stuff around (or allocating too many times small objects), not really from stack/heap choices of allocation.

Marco A.
  • 43,032
  • 26
  • 132
  • 246
0

When you are not sure (at compile time) about the amount of memory needed, dynamic allocation should be preferred (i.e. heap). Otherwise, use the stack.

Another point: in general stack size is comparatively much smaller than heap size. So, if a large amount of memory is needed (say, in MBs),then, even if the size is known, it is better to use the heap.

Siddhartha Ghosh
  • 2,988
  • 5
  • 18
  • 25
0

It is very unlikely that you are ever going to exceed the program stack size with function scoped variables. Even if you declare a lot of C++ objects inside a function, they would all have to be very big to exceed the stack limit. So I don't think this is a thing you have to worry about in your everyday coding. Declare C++ objects and native types as stack variables whenever it makes sense. If you do need to use dynamic allocations, always use a smart pointer or an STL container for collections (e.g.: std::vector).

Generally speaking, dynamic memory allocations are needed when you either have to extend the lifetime of an object beyond the scope where it was declared (e.g.: return an object from a function) or when the size of a collection, such as an array, is not known at compile time. Otherwise, you should prefer short lived/scoped objects.

glampert
  • 4,371
  • 2
  • 23
  • 49