Re: Stack vs. Heap

From: Peter van Merkerk (merkerk_at_deadspam.com)
Date: 10/09/03


Date: Thu, 9 Oct 2003 10:54:47 +0200


> I couldn't find an obvious answer to this in the FAQ. My basic
question,
> is: Is there any difference in allocating on the heap versus the
stack? If
> heap or stack implementation is not part of the standard, then just
> disregard this question. Here's some questions I'm confused about,
and if
> you can add anything else, please do so!
>
> Is the stack limited for each program?

The C++ standard doesn't say anything about stack or its size (other
than template class std::stack which is a different topic). But
practically speaking the stack size is limited. How big the stack size
is is depends on platform linkers settings...etc, in other words there
is no standard answer to your question.

> On the other hand, is the heap basically limitless (except of course
limited
> to the size of memory or page files)?

Typically the stack is smaller than the available free store (=heap).
Again the C++ standard doesn't say anything about this, so the answer
depends on what platform you are using.

> If I've got something on the heap, as I understand it, another program
can
> update any of my allocated storage without me knowing?

Since the C++ standard says nothing about running multiple processes on
the same system, this is an OS/platform issue not an C++ issue. OSes
like Linux and the 32-bit Windows versions normally do not allow one
program to write in the memory space of another program (though often
the OS does provide means to get around this barrier for example to
allow debuggers to change variables). Other OSes provide no protection
at all.

> Can this happen on the stack?

Since stacks typically reside in the same memory space as the heap the
same rules apply. If the OS/platform allows other programs to write to
the memory space of your program they can potentially damage the stack
as well. If you have a multithreaded program, each thread has its own
stack but typically share one heap.

> Is there any performance difference in using variables on the stack
versus
> on the heap?

Stack variable tend to be faster because most if not all the work to
allocate space on the stack can be done during compile time. For heap
allocation something always needs to be done during runtime.

> Are global and static variables on the stack?

No.

> The reason I ask is I'm starting to get into C#, and it makes a big
deal
> about allocating a lot of stuff on a garbage collected heap. I
wouldn't
> want to start asking off-topic questions, so I'll just ask this: would
a
> concept like this follow from the fact that heap storage is better to
use
> for some reason rather than storage on the stack?

No, the reason why C# more heavilly relies on heap is because it is
fundamentally different programming language than C++. The trade offs
are different and consequently things that make sense with C# (or Java
for that matter) do not necessarilly make sense in C++.

In C++ use stack objects where you can, use heap objects when you have
to. Besides efficiency of stack objects another (often even more
important) plus of stack objects is the automatic cleanup when they run
out of scope. Because C++ has no garbage collected heap, manually
creating and destroying objects on the heap is not only a pain, but also
difficult to get right i.c.w. exceptions.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


Relevant Pages

  • Off Topic: Stack vs. Heap
    ... Is there any difference in allocating on the heap versus the stack? ... > about allocating a lot of stuff on a garbage collected heap. ... > concept like this follow from the fact that heap storage is better to use ...
    (comp.lang.cpp)
  • Re: ALLOCATABLE arrays
    ... || If try to create a very large array on the stack and you do not have enough ... || Allocating on the heap gives you access to a hell of a lot more memory (well ... and "heap" (and there probably are/were some computers which don't/didn't ... | Automatic arrays are always allocated on the stack. ...
    (comp.lang.fortran)
  • 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: ALLOCATABLE arrays
    ... > If try to create a very large array on the stack and you do not have enough ... > Allocating on the heap gives you access to a hell of a lot more memory (well ... Allocatable arrays are allocated on the heap. ... Automatic arrays are always allocated on the stack. ...
    (comp.lang.fortran)
  • Re: Groovy (war Re: [PROST]Re: CDC Plugin fuer Eclipse)
    ... Warum müsste man continuations erlauben Stackvariablen in den Heap zu verschieben? ... Ein activation record speichert die Parameter und lokalen Variablen der Funktion und eine Information, ... Da diese activation records klassischerweise auf einem Stack verwaltet werden, kann man hier Stackspace sparen - Endrekursion ist damit genauso effizient wie eine klassische Iteration mit einem Sprung. ...
    (de.comp.lang.java)