Re: What's the difference between the heap and the freestore?

From: Jack Klein (jackklein_at_spamcop.net)
Date: 05/10/04


Date: Sun, 09 May 2004 23:54:22 -0500

On 9 May 2004 16:02:46 GMT, "John Harrison"
<john_andronicus@hotmail.com> wrote in comp.lang.c++:

>
> "Jorge Rivera" <jorgeri@rochester.rr.com> wrote in message
> news:lhsnc.17982$hY.13605@twister.nyroc.rr.com...
> > In general, when discussing dynamically allocated memory, I hear people
> > referring to the heap. As of a couple of days ago, I though the heap
> > was shared by new and malloc.
> >
> > I know that new must provide more information about the type of memory
> > allocated than malloc, etc. However, can somebody explain in more
> > details the differences between the heap and the freestore?
> >
> > I am interested in:
> >
> > 1. Do the heap and the freestore redside in different memory segments?
> > 2. What information is stored in the freestore?
> > 3. Why are the terms used interchangeably in most groups?
>
> When you use the term heap in the context of memory allocation it annoys
> some people because the C++ standard does not say that new uses 'the heap'.

There is no such thing as "the heap" defined anywhere, not just in the
C++ standard. The term "heap" is often used to refer to a pool of
memory from which allocations and deallocations are made. This is
usually associated with certain algorithms for managing those
allocations and de-allocations.

> That is not because there is anything wrong with new using the heap, its
> just because the C++ standard does not say anything about how new is
> implemented, its just talks about how it behaves.

Actually the C++ standard specifically uses the phrase "free store",
which even has its own section (12.5). But it does not in any way
define the term.

> In exactly the same way the C++ standard does not say anything about objects
> being allocated on 'the stack', it just says how automatic objects behave
> without saying that they have to be allocated on a stack or anything else.

If fact modern RISC processors, with large register sets, will quite
frequently place objects in registers, so long as their address is not
taken. Even if the processor has a hardware stack, not all automatic
objects will reside there.

> Despite this, everyone knows what is meant by 'the stack' and 'the heap' and
> 'free store' (which is exactly the same thing as the heap). In my view it
> would be better if people stopped making such fine distinctions that only
> end up confusing other people, which apparently has happened to you.

Yes, people who talk about the "stack" or the "heap" are the ones who
confuse the issue. The standard defines three storage classes:

  static

  dynamic

  automatic

If people would use the terms defined by the standard without delving
into implementation details, they would not confuse other people. It
makes no difference at all whether the implementation uses a stack, a
heap, virtual memory, or teleportation. Behavior is completely
defined in terms of these classes, not the methods used to implement
them.

The confusion is caused by those who introduce terms like "stack" and
"heap" into discussions which are completely defined by the three
standard terms above.

> Incidentally however, your assumption that new and malloc share something
> (call it 'the heap' or call it the 'free store') is not correct. Its quite
> likely to be true, but I don't think there are any guarantees.
>
> John

The latter point is quite interesting. Every conforming C++
implementation must provide std::malloc(), std::calloc(), std::free(),
and std::realloc() to any C++ program that calls them with a proper
prototype in scope. After years of evolution in C libraries, they are
likely to be highly efficient mechanisms for interfacing the C and C++
abstract machine with the underlying platform for obtaining and
releasing raw memory.

The earliest C++ implementations were preprocessing translators that
read C++ source code and output C code to be compiled by a C compiler.
They certainly used the C memory management functions to obtain the
raw memory used by new and delete.

It is still quite common today for C++ new and delete operators to use
malloc() and free() for their default allocation and deallocation, if
not overridden, but there is no requirement for them to do so.

The only connection between malloc() and operator new is that C++
specifically forbids malloc() from using operator new.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html


Relevant Pages

  • Re: run-time vs compile-time
    ... > offset related to some location (like stack base) somewhere. ... > offset from heap to pi. ... When you allocate an int on the heap, it is allocated at address 1. ... application has a given amount of memory it can use as it wishes. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: run-time vs compile-time
    ... > offset related to some location (like stack base) somewhere. ... > offset from heap to pi. ... When you allocate an int on the heap, it is allocated at address 1. ... application has a given amount of memory it can use as it wishes. ...
    (comp.lang.cpp)
  • Memory problems - WinDbg and SOS: Who recognizes this pattern?
    ... Last night I managed to get a memory dump using ADPlus and I analyzed it ... ephemeral segment allocation context: none ... Large object heap starts at 0x0a0d0030 ...
    (microsoft.public.dotnet.framework.performance)
  • 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: function "&" for strings type cause memory problem.
    ... > and I allocate the large object in heap memory in previous case. ... Memory allocation is always a risk. ... There is no difference between stack and heap here. ...
    (comp.lang.ada)