Stack vs. Heap

Discussion in 'C++' started by Kevin Grigorenko, Oct 9, 2003.

  1. Hello,

    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?

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

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

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

    Are global and static variables on the stack?

    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?

    I appreciate it!
    Kevin Grigorenko
    Kevin Grigorenko, Oct 9, 2003
    #1
    1. Advertising

  2. Off Topic: Stack vs. Heap

    Kevin Grigorenko wrote:

    >
    >
    > 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?
    >
    > On the other hand, is the heap basically limitless (except of course limited
    > to the size of memory or page files)?
    >
    > If I've got something on the heap, as I understand it, another program can
    > update any of my allocated storage without me knowing? Can this happen on
    > the stack?
    >
    > Is there any performance difference in using variables on the stack versus
    > on the heap?
    >
    > Are global and static variables on the stack?
    >
    > 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?


    It depends upon your compiler.
    Please tell us which compiler you are using
    so that we can redirect to to the appropriate newsgroup.
    E. Robert Tisdale, Oct 9, 2003
    #2
    1. Advertising

  3. Re: Off Topic: Stack vs. Heap

    "E. Robert Tisdale" <> wrote in message
    news:...
    > Kevin Grigorenko wrote:
    >
    > >
    > >
    > > 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?
    > >
    > > On the other hand, is the heap basically limitless (except of course

    limited
    > > to the size of memory or page files)?
    > >
    > > If I've got something on the heap, as I understand it, another program

    can
    > > update any of my allocated storage without me knowing? Can this happen

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

    versus
    > > on the heap?
    > >
    > > Are global and static variables on the stack?
    > >
    > > 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?

    >
    > It depends upon your compiler.
    > Please tell us which compiler you are using
    > so that we can redirect to to the appropriate newsgroup.
    >
    >


    All of those questions are non-standard? Ok, I have no doubts, you guys are
    the experts. Let's say the compiler I'm using is VC++6. 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.

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

    Thanks again!
    Kevin Grigorenko
    Kevin Grigorenko, Oct 9, 2003
    #3
  4. Re: Off Topic: Stack vs. Heap

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


    On Microsoft's news server (msnews.microsoft.com) -
    microsoft.public.dotnet.languages.csharp.
    Josephine Schafer, Oct 9, 2003
    #4
  5. Kevin Grigorenko

    Mike Wahler Guest

    Re: [FAQs, links] Off Topic: Stack vs. Heap

    "Kevin Grigorenko" <> wrote in message
    news:bm2qgn$1n38$...
    > "E. Robert Tisdale" <> wrote in message
    > news:...
    > > Kevin Grigorenko wrote:
    > >
    > > >
    > > >
    > > > 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?
    > > >
    > > > On the other hand, is the heap basically limitless (except of course

    > limited
    > > > to the size of memory or page files)?
    > > >
    > > > If I've got something on the heap, as I understand it, another program

    > can
    > > > update any of my allocated storage without me knowing? Can this

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

    > versus
    > > > on the heap?
    > > >
    > > > Are global and static variables on the stack?
    > > >
    > > > 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?

    > >
    > > It depends upon your compiler.
    > > Please tell us which compiler you are using
    > > so that we can redirect to to the appropriate newsgroup.
    > >
    > >

    >
    > All of those questions are non-standard?


    While often implemented with 'stacks' and 'heaps',
    the C++ standard makes no requirements (or prohibitions)
    that they be used for memory management in a C++
    program. Storage is only classified as 'automatic'
    (local variables), 'static' (file scope variables,
    or local variables or class members specifically
    made static with the 'static' keyword), and
    'allocated', a.k.a. 'dynamic' (objects created
    with 'new', 'new[]' , 'calloc()', 'malloc()' or
    'realloc()'

    How these classes of storage are actually implemented
    is, well, implementation defined.

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


    The skill level of people here varies widely, from
    complete novice to true 'gurus', some of whom are
    members of the ISO committee which standardized
    the C++ language. Even 'god' himself :), Bjarne
    Stroustrup, has been known to drop in from time to
    time. Spend some time here, and I think you'll
    quickly get a feel for "who's who."

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


    Here, it doesn't really matter which implementation
    (aka 'compiler') you use. C++ is a platform independent
    language. That's what the 'standard' is all about.

    > 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'm pretty sure there are links to the C# (and other
    MS stuff) newsgroups at www.msdn.microsoft.com

    www.usenet.org and www.groups.google.com also have
    newsgroup directories.

    >I couldn't find
    > anything to the effect of comp.lang.csharp.


    I think it's called
    microsoft.public.dotnet.languages.csharp

    but you can check the MSDN site to be sure.

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


    Yes. object storage in standard C++ is only
    classified as 'automatic', 'static', and
    'allocated'.

    Much good information about standard C++ here:
    http://www.parashift.com/c -faq-lite/


    Info about this newsgroup:
    http://www.parashift.com/c -faq-lite/


    Information about a learning newsgroup for C and C++
    (alt.comp.lang.learn.c-c++):
    http://www.contrib.andrew.cmu.edu/~ajo/faqs/acllc-c .html
    The "official" link is currently "out of order",
    this is a "mirror" of it (Thanks Arthur!)

    HTH,
    -Mike
    Mike Wahler, Oct 9, 2003
    #5
  6. > 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
    Peter van Merkerk, Oct 9, 2003
    #6
  7. Re: Off Topic: Stack vs. Heap

    "Kevin Grigorenko" <> wrote in message
    news:bm2qgn$1n38$...
    > > > [...]
    > > > 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
    David B. Held, Oct 9, 2003
    #7
  8. Kevin Grigorenko

    Mike Wahler Guest

    Re: Off Topic: Stack vs. Heap

    "David B. Held" <> wrote in message
    news:bm37v6$oa5$...
    > 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.


    Citations please.

    -Mike
    Mike Wahler, Oct 9, 2003
    #8
  9. Re: Off Topic: Stack vs. Heap

    "Mike Wahler" <> wrote in message
    news:Gv9hb.4024$...
    > "David B. Held" <> wrote in message
    > news:bm37v6$oa5$...
    > > 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.

    >
    > Citations please.


    Well, 12.5, for instance. The standard calls it the "free store",
    but I would be interested to see your argument for why that
    doesn't mean "program heap".

    Then there's 15.2/3, which talks about "stack unwinding". It's
    pretty hard to unwind a stack if you don't have a stack.

    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
    David B. Held, Oct 9, 2003
    #9
  10. Kevin Grigorenko

    tom_usenet Guest

    On Wed, 8 Oct 2003 23:22:14 -0400, "Kevin Grigorenko" <>
    wrote:

    >Hello,
    >
    >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?


    Yes, lots.

    If
    >heap or stack implementation is not part of the standard, then just
    >disregard this question.


    They aren't part of the standard, but the related concepts of
    "automatic storage" and "dynamic storage" (often implemented as a
    stack and heap respectively) are.

    >Is the stack limited for each program?


    Yes, usually, in a platform dependent manner.

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


    Yes, usually, but this is platform dependent.

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


    OS dependent. Modern OSes usually have protected memory spaces - each
    process can only access its own memory.

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


    A big one. Automatic storage is usually *much* faster than dynamic
    storage.

    >Are global and static variables on the stack?


    No. They are in "static storage", which may be allocated at program or
    module startup.

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


    With dynamic storage you can control the lifetime of the storage,
    whereas with automatic storage the storage is lost when the variable
    goes out of scope.

    However, the fact that automatic storage can be used for class objects
    as well as int, double, etc. is one of C++'s strengths. Because
    automatic storage is so much faster, this gives C++ an overwhelming
    performance advantage in some situations over languages (like Java and
    C#) that only allow allocation of class objects in dynamic storage. In
    addition, use of the stack allows C++ to provide a superior construct
    to finally - RAII through destructors.

    To summarize, use automatic storage whenever you can and dynamic
    storage only when you must.

    Tom
    tom_usenet, Oct 9, 2003
    #10
  11. Kevin Grigorenko

    Gavin Deane Guest

    Re: Off Topic: Stack vs. Heap

    "Kevin Grigorenko" <> wrote in message news:<bm2qgn$1n38$>...
    > Is the concept of stack and heap completely independent of the standard?


    The standard defines static, dynamic and automatic storage duration,
    which determine the life time of objects in your program. How those
    storage durations are implemented (stack, heap, some other system),
    whether there is a maximum amount of memory available, and whether
    other programs can interfere with memory you have used are all details
    specific to your implementation and operating system.

    GJD
    Gavin Deane, Oct 9, 2003
    #11
  12. Kevin Grigorenko

    osmium Guest

    Re: Off Topic: Stack vs. Heap

    "Kevin Grigorenko" writes:

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


    Formally, yes. But if someone from Mars were to implement a compiler they
    would likely invent the stack shortly after they started to write the code
    for the "auto" type. But they might use a different name for it.
    osmium, Oct 9, 2003
    #12
  13. Re: Off Topic: Stack vs. Heap

    osmium escribió:

    > Formally, yes. But if someone from Mars were to implement a compiler they
    > would likely invent the stack shortly after they started to write the code
    > for the "auto" type. But they might use a different name for it.


    I have read on www.perl.org that Parrot will use a linked list of
    garbage collected structures.

    Regards.
    =?iso-8859-1?Q?Juli=E1n?= Albo, Oct 9, 2003
    #13
  14. Kevin Grigorenko

    Gavin Deane Guest

    Re: Off Topic: Stack vs. Heap

    "David B. Held" <> wrote in message news:<bm39l9$odl$>...
    > "Mike Wahler" <> wrote in message
    > news:Gv9hb.4024$...
    > > "David B. Held" <> wrote in message
    > > news:bm37v6$oa5$...
    > > > 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.

    > >
    > > Citations please.

    >
    > Well, 12.5, for instance. The standard calls it the "free store",
    > but I would be interested to see your argument for why that
    > doesn't mean "program heap".
    >
    > Then there's 15.2/3, which talks about "stack unwinding". It's
    > pretty hard to unwind a stack if you don't have a stack.


    Though perhaps not impossible, since "stack unwinding" is defined
    without "stack" being defined.

    GJD
    Gavin Deane, Oct 9, 2003
    #14
  15. Kevin Grigorenko

    WW Guest

    Re: Off Topic: Stack vs. Heap

    Gavin Deane wrote:
    >> Then there's 15.2/3, which talks about "stack unwinding". It's
    >> pretty hard to unwind a stack if you don't have a stack.

    >
    > Though perhaps not impossible, since "stack unwinding" is defined
    > without "stack" being defined.


    The following standards contain provisions which, through reference in this
    text, constitute provisions of this International Standard...

    - ISO/IEC 2382 (all parts), Information technology - Vocabulary

    So it seems stack does not need to be defined in the C++ standard.

    --
    WW aka Attila
    WW, Oct 9, 2003
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. joe

    stack and heap question

    joe, Aug 4, 2004, in forum: Java
    Replies:
    3
    Views:
    429
    Michael Borgwardt
    Aug 5, 2004
  2. Steve Hill

    vector.swap and heap / stack

    Steve Hill, Aug 20, 2003, in forum: C++
    Replies:
    5
    Views:
    999
    tom_usenet
    Aug 22, 2003
  3. Michal Slocinski

    Heap dump file size vs heap size

    Michal Slocinski, Mar 25, 2008, in forum: Java
    Replies:
    1
    Views:
    732
    GArlington
    Mar 25, 2008
  4. viki
    Replies:
    6
    Views:
    561
    Erik Wikström
    Jun 28, 2008
  5. Raymond Schanks
    Replies:
    0
    Views:
    518
    Raymond Schanks
    Apr 11, 2010
Loading...

Share This Page