free() and delete

M

Michael

Hi,

free() and delete are both used to release ptr memory. Any difference.
When shall I use which?

Thanks in advance,
Michael
 
N

Noah Roberts

Michael said:
Hi,

free() and delete are both used to release ptr memory. Any difference.
When shall I use which?

Use new/delete unless you have a really good reason to use
malloc/realloc/free.

Use delete when you used new, use free when you used malloc.
 
N

Noah Roberts

Michael said:
Hi,

free() and delete are both used to release ptr memory. Any difference.
When shall I use which?

Oh, and delete calls a destructor, if any, and free does not.
 
T

Thomas J. Gritzan

Michael said:
Hi,

free() and delete are both used to release ptr memory. Any difference.
When shall I use which?

Simple rules:

- Memory allocated with malloc() has to be freed with free(),
- Objects that are new'ed have to be delete'd,
- Arrays of Objects, created with new[], must be destroyed with delete[].

Some guidelines:

- In C++, don't ever use malloc() or free(),
- Instead of new[] you are safer using a container (std::vector<> for
example)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top