Difference between new and malloc in C++ – The answer of interview question C++ malloc vs new heap memory allocation includes advantages of new operator over malloc function in C++ programming language. Also, discussion about performance of new and malloc, free() function and delete operators are included.
Note that in a technical interview, generally, it is asked to tell the differences between new operator and malloc function with no other conditions specified. But, some interviewer may dive deeper and can ask about performance , uses etc. of malloc and new operator in C++ programs. The performance discussion from one of the interview is also included in the answer.
C++ malloc vs new operator
- new operator constructs the object and initializes allocated memory from heap memory by calling constructor where as malloc function does not, and allocate uninitialized memory.
- new can be overloaded but malloc cannot be.
- C++ syntax of using new is easy e.g int*p = new int; where as for malloc it is little bit complex e.g int *p= (int*)malloc(sizeof(int));
- new returns proper typed pointer and malloc returns void pointer which needs to be typecast to the desired type as above.
- No need to calculate the size of memory using sizeof() operator in case of “new”.
- new throws an exception on memory allocation failure and malloc returns NULL.
- heap memory allocated by new cannot be resized while memory allocated by malloc can be resized using realloc function.
Performance discussion in an interview– on new and malloc and free and delete
This discussion from one of the technical interview.
Interviewer: Which is slower between new operator or malloc function?
In fact, new operator is bit slower than malloc function as new operator performs extra operation in C++ program i.e. new operator calls constructor of the class besides dynamic memory allocation. Similarly, delete operator calls destructor of the class besides dynamic memory allocation whereas free() function does not.
Interviewer: So, do you think that we must always use malloc() function not new operator for dynamic memory allocation from heap memory in C++ projects to get a better performance?
No, it’s not like that. The new and delete operator in C++ language are a powerful feature for dynamic memory allocation and de-allocation that cannot be ignored.
Above differences already tells the advantages of new and delete operators over malloc and free () functions.
Bit slower does not mean that we should not use new operator for memory allocation. In practice, it is not going to make C++ applications very slow. However, if we are very cautious about the performance, we can use malloc() functions for memory allocation for inbuilt data type i.e. integer and array etc within C++ class functions body.
Interviewer: Ok, tell me one situation where new and delete is indispensable.
One of the situations is overloading new and delete operators in C++ program for custom dynamic memory allocation.
Interviewer: What do you mean by overloading new and delete and custom memory allocation?
Interviewer: Any critical issue if can we get if we use free () function operator instead of delete operator to destroy objects?
Yes, memory leak issue we may get in this case.
It is best practice to use new and delete operators to dynamically allocate memory for class objects in C++ Object oriented programing to avoid potential memory leaks. May be in some of the class destructor, you may have de-allocated resource e.g. file handling or other class memory de-allocation etc. So, if we don’t use delete but free () function, it will not call class destructor and memory leak will happen.
It is important to note that memory leaks drastically make the application slow and also application can be freeze or hang.