15 Nov 2019

3 Most Important Differences between malloc() and new operator in C++

Often in C++ Programs we use new operator to initialise an object or a pointer. Also as we know malloc() is also used for the same purpose then what is the difference between these two ? Or really is there any difference or not? So here is the answer.

Sr. No.New Operatormalloc()
1However it is a feature of C++ and not in C – which is used to initialize a class object or a pointer.malloc() is a library function and it was used in C/C++ language to allocate memory for n blocks at run time, it can also be used in c++ programming language. whenever a program needs memory to declare at run time we can use this function.
2When you use new operator at that the constructor of the class is automatically executedIn case of malloc() the constructor is not executed automatically.
3New works faster as compared to malloc()Malloc() being a function works slowly as compared to new operator.