Alex Rivera | Logout

In what cases do I use malloc and/or new?

Asked 2008-10-08T19:47:55.090
629

I see in C++ there are multiple ways to allocate and free data and I understand that when you call malloc you should call free and when you use the new operator you should pair with delete and it is a mistake to mix the two (e.g. Calling free() on something that was created with the new operator), but I'm not clear on when I should use malloc/ free and when I should use new/ delete in my real world programs.

If you're a C++ expert, please let me know any rules of thumb or conventions you follow in this regard.

Edit
Report

1 Answer

3

From a lower perspective, new will initialize all the memory before giving the memory whereas malloc will keep the original content of the memory.

answered 2011-08-14T20:31:35.713

Your Answer