new
/delete
keywords are use the free store
malloc
/free
keywords are use the heap
i see written somewhere that new
use malloc
. how could it be? they are not use in the memory segment?
The "free store" in C++ is a term that describes the place where objects of dynamic storage duration live. The term "the heap" is a colloquialism for the same thing. new
may use malloc
and that's fine.
However, it does other stuff too, such as invoking constructors and, potentially, instead of malloc
using some other memory management system (such as a memory pool).
Yes, it is undefined to free
an object that you allocated with new
, primarily because free
does not invoke class destructors. Similarly for delete
ing something you created with malloc
: to do so would be to mix two different allocation/construction idioms and, although you can make it work, it would be spectacularly difficult to do so robustly.
why this memory segment have two names?
Because "the heap" (which is not even a "memory segment") may not be a heap so it's a stupid term. C++ tried to phase it out by introducing the abstraction "free store". Unfortunately, people keep saying "the heap" because of (a) legacy, (b) copy-cat bikeshedding, (c) ignorance of the purpose/meaning/value of abstraction, and (d) poor education/instruction.