Parag
Parag

Reputation: 7946

Why use malloc/free, when we have new/delete?

What is the use of malloc and free when we have new and delete in C++. I guess function of both free and delete is same.

Upvotes: 5

Views: 3368

Answers (3)

James Kanze
James Kanze

Reputation: 153919

First, when you speak of new and delete, I assume you mean the expressions, and not the operator new and operator delete functions. The new and delete expressions are not related to malloc and free, and only manage memory incidentally; their main role is to manage object lifetime: a new expression will call the operator new function to obtain memory, and then call the constructor; a delete expression will call the destructor before calling operator delete to free the memory. For the most part, objects should be created, and not simply allocated, which means using the expressions exclusively.

There are some rare cases where one wants to separate allocation and initialization (creation); implementing things like std::vector is a classical example, where you'll allocate for many objects in one go, but only construct one at a time. In such cases, you'll use the operator new function for allocation, and placement new for initialization; at the other end, you'll explicitly call the constructor (something like p->~T()) for destruction, and use the operator delete function to free the memory.

Off hand, I can only think of two cases where you'd use malloc and free in C++. The first is to implement your own replacements of the ::operator new and ::operator delete functions. (I often replace the global ::operator new and ::operator delete with debugging versions, which trace allocations, put guard zones around the allocated memory, etc.) The other is when interacting with a legacy library written in C: if the library says to pass a pointer to memory allocated by malloc (because it will free it itself using free), or more commonly, returns a pointer to memory allocated by malloc, which you're expected to free, then you must use malloc and free. (The better libraries will provide their own allocation and deallocation functions, which do more or less what the new and delete operators do, but there will always be things like strdup().)

Upvotes: 3

Alok Save
Alok Save

Reputation: 206526

In C++, it is rarely useful that one would use malloc & free instead of new& delete.

One Scenario I can think of is:

If you do not want to get your memory initialized by implicit constructor calls, and just need an assured memory allocation for placement new then it is perfectly fine to use malloc and free instead of new and delete.

On the other hand, it is important to know that mallocand new are not same!
Two important differences straight up are:

  • new guarantees callng of constructors of your class for initializing the class members while mallocdoes not, One would have to do an additional memset or related function calls post an malloc to initialize the allocated memory to do something meaningful.

  • A big advantage is that for new you do not need to check for NULL after every allocation, just enclosing exception handlers will do the job saving you redundant error checking unlike malloc.

Upvotes: 5

Luchian Grigore
Luchian Grigore

Reputation: 258608

They're not the same. new calls the constructor, malloc just allocates the memory.

Also, it's undefined behavior mixing the two (i.e. using new with free and malloc with delete).

In C++, you're supposed to use new and delete, malloc and free are there for compatibility reasons with C.

Upvotes: 8

Related Questions