Re: Off Topic: Stack vs. Heap
From: David B. Held (dheld_at_codelogicconsulting.com)
Date: 10/09/03
- Next message: Mike Wahler: "Re: Off Topic: Stack vs. Heap"
- Previous message: Mike Wahler: "Re: BREAKING NEWS: WMD found"
- In reply to: Kevin Grigorenko: "Re: Off Topic: Stack vs. Heap"
- Next in thread: Mike Wahler: "Re: Off Topic: Stack vs. Heap"
- Reply: Mike Wahler: "Re: Off Topic: Stack vs. Heap"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Mike Wahler: "Re: Off Topic: Stack vs. Heap"
- Previous message: Mike Wahler: "Re: BREAKING NEWS: WMD found"
- In reply to: Kevin Grigorenko: "Re: Off Topic: Stack vs. Heap"
- Next in thread: Mike Wahler: "Re: Off Topic: Stack vs. Heap"
- Reply: Mike Wahler: "Re: Off Topic: Stack vs. Heap"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|