FAQ 12.04 Can pointers returned from new be deallocated with free() ? Can pointers returned from malloc() be deallocated with delete ?

FAQ 12.04 Can pointers returned from new be deallocated with free()? Can pointers returned from malloc() be deallocated with delete?

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() on a pointer allocated via new or to call delete on a pointer allocated via malloc().

Even if it appears to work on your particular compiler on your particular machine, please don't do it. Corrupting the heap is a very subtle and disastrous thing; it's just not worth the trouble even if the data type is a simple array of char; even if some programmers think it would be cool. Just say no.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net