Re: stack or heap for C++ object
On Jul 1, 1:06 am, moongeegee <moongee...@gmail.com> wrote:
How to determine we should use either stack/static storage or
heap for C++ objects.
Use local variables (stack) when you can. Use dynamic
allocation (heap) when you have to. Some of the reasons which
might mean you have to:
-- The object has identity (can't be copied), and a lifetime
which doesn't correspond to that of local scope.
-- The object is polymorphic, and the exact type won't be known
until runtime.
-- The object is too big to fit on the stack. (This is a
purely technical issue, which means compromising with the
design.)
-- The object is too expensive to copy, and its value is needed
beyond local scope. (This is a purely optimization issue,
and shouldn't be invoked unless the profiler says you have a
problem. And there are often other solutions which are
better.)
I know there are pro and con between stack/static storage and
heap.
I don't know if you can speak of pros and cons. They server
different purposes.
If I have a program which is written by other programmers, how
to detect whether the program use stack or heap.
Why do you have to know? What difference does it make?
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34