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

J

Jorge Rivera

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

John Harrison

Jorge Rivera said:
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'.
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.

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.

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.

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
 
J

Jorge Rivera

Thanks for the insightful comments.
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.

What I though was that both new and malloc allocated memory in a common
memory segment, with the notorious difference that new not only
allocated the requested bytes, it needed to add information necesary for
delete to work properly. In my mind, the difference between new and
delete was just behavioral, and it had nothing to do with where the
memory comes from...

Thanks,

JLR
 
C

Claudio Puviani

Jorge Rivera said:
Thanks for the insightful comments.


What I though was that both new and malloc allocated memory in a common
memory segment,

Not necessarily, which is precisely why -- contrary to John Harrison's comment --
the two are NOT considered to be the same thing. It would be perfectly valid and
standard-conforming for a compiler to use two separate areas of memory, or
different internal organization, for the heap and the free store. That it's
usually not done doesn't change the fact that it can be. This is similar to the
common practice in 32-bit compilers to make ints and longs the same size. They're
still different types and if a compiler chose to make longs 64 bits wide, it
would be a perfectly valid (and probably welcome) choice.
with the notorious difference that new not only allocated the requested
bytes, it needed to add information necesary for delete to work properly.

The standard allows for more important differences.
In my mind,

Well, we'd rather defer to the standard. :)
the difference between new and delete was just behavioral, and it had nothing
to do with where the memory comes from...

And you happen to be wrong. It's not a personal attack; just an observation. The
standard is limpid on the fact that the malloc and new need not share a common
memory pool and personal opinions, preconceptions, or specific implementations
just don't enter into the equation.

Claudio Puviani
 
J

Jack Klein

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

John Harrison

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.

Well I don't think so. For one thing those terms are clearly in common use
outside of this group, and people come to this group with a reasonable
understanding of what those terms mean. Also other languages, even if they
are very different from C++, also have similar terms. In many languages one
can quite reasonably talk about heap allocation and stack overflow etc. etc.
so clearly these terms express some commonality that is found in many
implementations even though they aren't in the C++ standard.

john
 
O

osmium

Jack said:
There is no such thing as "the heap" defined anywhere, not just in the
C++ standard.

I don't think you looked very hard before reaching your conclusion.

"A _heap_ is defined to be a complete binary
tree with the property that the value of each
node is at least as large as the value of its
children nodes."

Note the italics provided in the quote. That is not a definition I would
have chosen but it is *a* definition by someone qualified to *make*
definitions.

It is from _Fundamentals of Data Structures_, p. 357, by Horowitz and
Sahini.

I assume you are picking on the distinction between "the" heap and "a" heap.
If so, you are doing a disservice to many people who are already struggling
with the English language in this newsgroup by making your point in such a
subtle way. Is it really that important to you to make your point?
 
C

Claudio Puviani

osmium said:
I don't think you looked very hard before reaching your conclusion.

"A _heap_ is defined to be a complete binary
tree with the property that the value of each
node is at least as large as the value of its
children nodes."

Note the italics provided in the quote. That is not a definition I would
have chosen but it is *a* definition by someone qualified to *make*
definitions.

It is from _Fundamentals of Data Structures_, p. 357, by Horowitz and
Sahini.

I assume you are picking on the distinction between "the" heap and "a" heap.
If so, you are doing a disservice to many people who are already struggling
with the English language in this newsgroup by making your point in such a
subtle way. Is it really that important to you to make your point?

This definition isn't applicable in this context. You chose a definition for the
data structure called a "heap". In the context of memory management, a "heap"
refers to an area of memory from which dynamic allocations are made and it has no
relation to the "heap" data structure. It's unfortunate that the name has been
overloaded to represent two entirely unrelated concepts, but the two meanings
have coexisted for at least 20 years (and I suspect much longer), so it's we who
have to adapt. As is the case with any vernacular, those who are less fluent with
the language _will_ inevitably find it more difficult, but that's just life.

Claudio Puviani
 
D

Dave Moore

Jorge Rivera said:
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?

As I understand it, there is no conceptual difference between the
so-called 'heap' and 'free-store'. They are both used to refer to the
pool of memory from which a C++ program allocates storage for dynamic
objects. OTOH, according to Herb Sutter's GotW (I think #9), there is
a conventional difference between them in the C/C++ community, in that
'free-store' and 'heap' are generally used to refer to the pools of
memory from which operator new and malloc allocate objects,
respectively. One important thing to note is that these pools are not
required to be identical ... AFAIK they can in practice be distinct,
identical or overlapping. That is why you sometimes see questions
about whether memory allocated for objects using new can be
de-allocated using free (it is not safe practice to do so).

Now, I will say that is was old GotW, which pre-dates the Standard,
but I have since seen the conventions used elsewhere.

HTH, Dave Moore
 
J

Jorge Rivera

Thanks to all for the comments.

I will summarize all your comments to see weather I got this right or not.

1. C++ standrad talks about the 'free store,' not the 'heap'.
2. The standard does not require new to allocate memory from the same
memory pool than malloc and its brethren.
By consequence of 2, some implementations ues the same memory pool (new
implemented by calling malloc, for example), whereas there might be
others that don't.
3. In practice, people keep referring to dynamically allocated memory
as memory created in the 'heap'. This commonly used term may not be
correct according to the standard, but it is well understood that it
stands for dynamically allocated memory.

If all this is all there is to it, n my mind this is closed...

Jorge L.
 
J

John Harrison

Jorge Rivera said:
Thanks to all for the comments.

I will summarize all your comments to see weather I got this right or not.

1. C++ standrad talks about the 'free store,' not the 'heap'.
2. The standard does not require new to allocate memory from the same
memory pool than malloc and its brethren.
By consequence of 2, some implementations ues the same memory pool (new
implemented by calling malloc, for example), whereas there might be
others that don't.
3. In practice, people keep referring to dynamically allocated memory
as memory created in the 'heap'. This commonly used term may not be
correct according to the standard, but it is well understood that it
stands for dynamically allocated memory.

If all this is all there is to it, n my mind this is closed...

Jorge L.

I agree with all three points.

john
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top