Re: Off Topic: Stack vs. Heap

From: David B. Held (dheld_at_codelogicconsulting.com)
Date: 10/09/03


Date: Thu, 9 Oct 2003 03:56:48 -0500


"Kevin Grigorenko" <kzg110@psu.edu> wrote in message
news:bm2qgn$1n38$1@f04n12.cac.psu.edu...
> > > [...]
> > > Is there any difference in allocating on the heap versus
> > > the stack?

Yes. It's one of the most fundamental storage issues in
programming. In C++, stack-allocated variables have
"automatic" storage, which means that their storage is
allocated automatically, and they are destroyed
automatically when they go out of scope. Heap-allocated
data is allocated explicitly and destroyed explicitly, which
allows you to control its lifetime.

> > > [...]
> > > Is the stack limited for each program?

That's implementation-dependent. Some systems have a
fixed-size stack, and others will grow the stack for you up
to a limit. All systems will experience a stack overflow if
you call a recursive function with no terminating condition.
In that technical sense, the answer is "yes". ;>

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

C++ does not specify any limit on the heap size. However,
your heap will be limited by the address space of your
hardware, at the least.

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

If it can get a reference into your storage, yes. Whether that
is allowed depends on your operating system, not your
programming language.

> > > Can this happen on the stack?

Implementation-defined.

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

Heap allocation is almost always more expensive than
stack allocation, and often by a significant margin.

> > > Are global and static variables on the stack?

They are typically allocated their own (non-stack) storage.

> > > 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.

Garbage collection is a whole different ball of wax. The
allocation/collection times may be totally different for a
collected system than an explicitly allocated system. So
getting answers about C++ won't necessarily tell you about
C#. Whether you pay a performance penalty depends on
the nature of your application. One can always produce
pathological test cases for any allocation scheme. It
also depends on your requirements.

> > > [...]
> > > 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?

Generally, the so-called "4GL" languages tend to prefer
heap allocation because it makes dealing with references
simpler. If everything lives on the heap, then you can't end
up with a dangling reference. It may also simply the design
and implementation of the language in other ways. It is
certainly not because heap allocation with collection is
always a better method.

> [...]
> All of those questions are non-standard?

Not at all. Many of them are relevant to C++.

> Ok, I have no doubts, you guys are the experts.

You can't assume that anyone who posts here is an expert
(me included!).

> Let's say the compiler I'm using is VC++6.

Don't forget to always start your programs with "void main()". ;>

> Also, I was looking for a C# newsgroup, so if you could
> also direct me to where I can post questions regarding
> that, that would be great. I couldn't find anything to the
> effect of comp.lang.csharp.

It's not exactly as popular as other older languages. Whether
that is a good or bad thing is up to you to decide.

> Is the concept of stack and heap completely independent
> of the standard?

Many languages utilize the concept of a stack and program
heap (most stack-based languages, for instance). But if you
are asking if the C++ standard talks about them, it most
certainly does. Otherwise, it would have a peculiar time
explaining operator new and delete.

Dave

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003


Relevant Pages

  • Re: variable storage
    ... memory segments like stack heap and its allocation. ... All auto variables are placed into automatic storage. ... Stack and heap do not appear in the standard. ...
    (comp.lang.c)
  • Re: DLL pass vector by value crash
    ... I can't see why the issue of common heap and allocator (operator ... Presuming that you build your exe and dll with same compiler and same ... allocation functions are called, ...
    (microsoft.public.vc.mfc)
  • Re: Whats the difference between the heap and the freestore?
    ... >> In general, when discussing dynamically allocated memory, I hear people ... As of a couple of days ago, I though the heap ... > When you use the term heap in the context of memory allocation it annoys ... > without saying that they have to be allocated on a stack or anything else. ...
    (comp.lang.cpp)
  • Re: Tip Required on Dynamic Memory Allocation in Server Applications ...
    ... a memory allocation failure after 300 usages of the string ... Suggestions where given to pre-reserve the size of the heap, ... My program works untill 200-300 requests are ...
    (microsoft.public.win32.programmer.kernel)
  • Re: GC.Collect: Exactly how does it work?
    ... MB segment for the LOH. ... At the start of a process the GC heap looks like this: ... filled (committed regions) and a third segment had to be created. ... the allocation scheme and the frequency of most recent ...
    (microsoft.public.dotnet.languages.csharp)