Question

Whilst asking another question (and also before) I was wondering how do I judge whether to create an object on the heap or keep it as an object on the stack? What should I ask myself about the object to make the correct allocation?

Was it helpful?

Solution

Put it on the heap if you have to, the stack if you can.

What kinds of things do you need to put on the heap? Anything of varying length. Any object that might need to be null. Anything that's very large, lest you cause a stack overflow.

OTHER TIPS

Simple answer.

When it goes out of scope, do you want it to hang around and be able to use it?

Depends on intended lifetime of the object.

  • If you want the object to be alive even after function returns, then HEAP, else STACK

If an object is placed in the HEAP, then it must be explicitly free()'ed or deleted by the programmer, once its usage is over; otherwise the program will be leaking memory.

Two reasons to use the heap:

1- You want the data after the current scope.

2- You want to reserve large memory.

Other than that stay on stack.

Note: don't reserve a lot of memory on the stack, or you'll get a "Stack-overflow" ;)

Stack memory is fast. It is fast because (a) there is no system overhead to allocate the memory - the allocation is done by simply moving the stack pointer in one instruction and (b) the memory in the stack is "hot" so it is already in cache. Heap memory is slow because (a) it requires a lot of system work to look around and find a free chunk of memory and (b) is probably not in cache and will require evicting some data you might have wanted.

Stack memory doesn't get fragmented. It is possible that a heap eventually gets so fragmented, you can't allocate anything (even though ironically there is still enough unused memory!)

For long lived data and for large data (multi KB or more), you have to use a heap.

The danger of allocating a bigger stack is that it might hurt you if are running multiple threads. You have to size the stack for the "worst case" usage. Each thread requires its own stack. On a high core count machine (where you might have 200+ threads running), you may not want to arbitrarily increase the stack. The heap on the other hand does not need to be sized for "worst case" usage - it is much more efficient.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top