0

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?

second, i see written somewhere that we can't use free after new or use delete after malloc. but if new uses malloc why free will not work?

and where we use free after new ect'. this is an error or undefined behavior?

thanks

asaf app
  • 384
  • 2
  • 3
  • 12

3 Answers3

6

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

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • why this memory segment have two names? – asaf app Apr 02 '14 at 11:50
  • 1
    @asafapp: Because "the heap" 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. – Lightness Races in Orbit Apr 02 '14 at 11:52
  • because the standard likes to keep things abstract, and not tie things to a concrete implementation. and e) because in practice, it's the heap. – Karoly Horvath Apr 02 '14 at 11:52
  • 2
    Also, *it's not a memory segment*. It's just an abstract name for free memory, regardless of where it comes from or whether it all comes from the same place. – Steve Jessop Apr 02 '14 at 11:52
  • @Lightness Races in Orbit. people say that new use free store and malloc use the heap. if they are the same. why to do that complicate – asaf app Apr 02 '14 at 12:01
  • 2
    @asafapp: Because people are silly and the world is big. There is a _lot_ of misinformation out there. – Lightness Races in Orbit Apr 02 '14 at 12:11
3

Regarding your first question: Free store and heap are synonyms. See for example: http://en.wikipedia.org/wiki/Heap_%28programming%29. So there is no contradiction there.

As for why not to mix new and delete, this answer: https://stackoverflow.com/a/3184129/2050788 gives an excellent summary. Copy pasted for your convenience:

If you do so you will run into undefined behavior. Never try that. Although new might be implemented through malloc() and delete might be implemented through free() there's no guarantee that they are really implemented that way and also the user can overload new and delete at his discretion. You risk running into heap corruption. Other than that don't forget that when you call malloc() you get raw memory - no constructor is invoked - and when you call free() no destructor is invoked. This can as well lead to undefined behavior and improper functioning of the program. The bottom line is... never do this.
Community
  • 1
  • 1
Spacemoose
  • 3,856
  • 1
  • 27
  • 48
1

In short: Never combine these memory management methods. That is, never use free on memory allocated via new and vice versa.

Now some C++ standard library implementations do in fact implement operator new using malloc, so it might work in some cases on some platforms, but you mustn't rely on that, as an implementation is neither guaranteed nor required to do so.

Additionally, free will not call destructors, and then, C++ classes may provide specialized memory management, which will definitely break if you mix it with malloc/free.

lethal-guitar
  • 4,438
  • 1
  • 20
  • 40