Re: Stack vs. Heap

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Typically, if an element does not need to exist beyond the scope of its variable, you are
much better off allocating it on the stack instead of the heap. Objects which need to
have a lifetime beyond the local scope must be allocated on the heap. It is common to
allocate objects whose size is not known at compile time on the heap, although in this
case the preferred method depends on the lifetime; for values needed within the local
scope, CArray or std::vector are a better choice than a raw malloc or new because the
destructors of these objects ensure that the object is properly deallocated when you leave
scope.

An example of poor strategy:
CMyDIalog * dlg = new CMyDialog;
dlg->DoModal();
...
delete dlg;

the corresponding good strategy is
CMyDialog dlg;
dlg.DoModal();
...

A poor strategy
LPBYTE b = new BYTE[size];
...
delete b;

A good strategy
CByteArray b;
b.SetSize(size);
or
std::vector<BYTE> b;
b.resize(size);

A poor strategy
LPDWORD d = new DWORD;
*d = 0;
SomeAPI(d);
...
delete d;

A good strategy
DWORD d = 0;
SomeAPI(&d);
...

A failing strategy
TCHAR buffer[COMPILE_TIME_SIZE];
read_stuff_into(buffer);
PostMessage(UWM_HERES_DATA, (WPARAM)buffer);

A working strategy
TCHAR * buffer = new TCHAR[COMPILE_TIME_SIZE];
read_stuff_into(buffer);
PostMessage(UWM_HERES_DATA, (WPARAM)buffer);

where the OnHeresData handler does a delete of the TCHAR pointer passed via WPARAM:

LRESULT CMyClass::OnHeresData(WPARAM wParam, LPARAM)
{
TCHAR * buffer = (TCHAR *)wParam;
... use it
delete buffer;
return 0;
}
The above case shows that the object needs to exist beyond the scope of the function in
which it is declared.

Bottom line: if you don't need to put it on the heap, don't put it on the heap. If you
need to put it on the heap (as CArray and std::vector do for their contents) avoid
situations in which an explicit delete is required. If you need it to exist for a long
time, put it on the heap.
joe

On Thu, 10 Jan 2008 11:30:31 +0200, "Dan" <dan@xxxxxxxxx> wrote:

Is there a simple rule when to define objects on the heap and when to do it
on the stack?

Regards

Dan


Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: How does managed code work?
    ... Does it work the same way as the native stack with a frame pointer that is the head of a linked list of stack frames where each time we enter a function we create a new stack frame in which new variables are pushed and each time we exit a function the entire stack frame is popped? ... Can someone point me to a discussion of the managed heap? ... How does it prevent memory leaks that occur in COM when two objects reference each other and keep the others reference count nonzero? ... Because objects don't go out of scope, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: difference between stack & heap (general, for a newbie)
    ... The stack is used to allocate temporary objects while the heap is used by a ... an object in a heap is not scope ... an object on the stack is ... destroyed automatically when its scope ends. ...
    (comp.lang.cpp)
  • Re: Garbage collection in VBA
    ... For local variables, Yes. ... > Set myRange = Something ... > Or is the memory allocated in the function returned to the heap when the ... > function goes out of scope? ...
    (microsoft.public.excel.programming)
  • Re: Stack vs. heap?
    ... that requires it comes into and goes out of scope. ... Global variables are different than the heap - or perhaps global ... global to the program that allocates them (dependent on language ... An OS may support objects of even greater scope (persistence) than ...
    (comp.sys.apple2)
  • Re: difference between stack & heap (general, for a newbie)
    ... understand how these two data structures are implemented when your C++ ... What I should've asked is "what is the scope of data when allocated ... stack (where ``heap'' and ``stack'' are not standards, ... allocated on the stack versus those allocated on the heap, ...
    (comp.lang.cpp)