1 2 3 4 5 6 7 8 9 10 11 12

The following material is from the C++FAQ-Lite home page

[16.2] Can I free() pointers allocated with new?
Can I delete pointers allocated with malloc()?
No!
It is perfectly legal, moral, and wholesome to use malloc() and delete in the same program, or to use new and free() in the same program. But it is illegal, immoral, and despicable to call free() with a pointer allocated via new, or to call delete on a pointer allocated via malloc().

[16.3] Why should I use new instead of trustworthy old malloc()?

Constructors/destructors:
Unlike
malloc(sizeof(Fred)),new Fred()calls Fred's constructor.
Similarly,
delete pcalls *p's destructor.
Type safety:
malloc() returns a void* which isn't type safe.
new Fred() returns
a pointer of the right type (a
Fred*).
Overridability:
new is an operator that can be overridden by a class, while malloc()
is not overridable on a per-class basis.

24-Mar-98

Page 8

TAM/C201