问题
Originally a pedantics war on @Als answer here, it also sparked a discussion in the C++ chatroom.
This article by Herb Sutter distinguishes between the two, but is also over a decade old, as it clearly was written before 2000 and also talks about the standard draft, which can only mean C++98 draft. Though, I still expect Herb, as part of the committee, to be knowledgeable about this stuff.
I know of this and this question/answer, but the second just cites Herb's article while the first cites another source that simply rejects the use of the term "heap", aka they are both not exactly satisfactory. Also, I can't find any free-store
or freestore
tag on SO (until this question, I created the former one now).
Now, what is really the difference, if any?
回答1:
Well, the current C++ standard only uses the term "free store" - the only use of "heap" in the Standard is to describe the heap data structure in the Standard Library. So "heap" is not a very useful term to use when trying to discuss C++ problems accurately, though of course everyone does it.
回答2:
In Herb's book "Exceptional C++", he defines:
Heap: A dynamic memory area that is allocated/freed by the malloc
/free
functions.
Free Store: A dynamic memory area that is allocated/freed by new
/delete
.
Its possible for new
and delete
to be implemented in terms of malloc
and free
, so technically, they could be the same memory area. However, as the standard doesn't specify this, its best to treat them separatly, and not to mix malloc
/delete
or new
/free
.
回答3:
The free-store
technically isn't the heap, just as local variables technically aren't on the stack. However, it's extremely rare in my experience to find anybody who won't accept those terms.
来源:https://stackoverflow.com/questions/6161235/what-is-the-difference-between-the-heap-and-the-free-store