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.