Difference Between Malloc and New
Malloc vs New
One of the most common interview questions in hiring a computer programmer is for the applicant/computer programmer to explain the differences between malloc and new. Both malloc and new exist in the realm of computer language and often are used by computer programmers for dynamic memory allocations.
The basic differences between the two is that malloc exists in the C-language while new is a specific feature of the C++ language. Malloc is also a function (which, for programmers, requires some time to execute) while new is an operator program (which cuts the execution time). This is a benefit from the operator new because programmers doing real-time programming have a need to choose the fastest method to use.
Operator new is type-safe unlike the function malloc.
Function malloc is library function for the C-language. All it does is allocate memory and return a pointer to it. On the other hand, operator new is a language-level construct, and its job is to instantiate an object by allocating memory and calling the appropriate constructors. Another difference of function malloc from operator new is that malloc requires typecasting during declaration while the former doesn’t. Also, function malloc uses the word “free” to clear the memory while “delete” is used in operator new for the similar purpose.
The two also have different ways in dealing with failure and memory exhaustion. If function malloc fails, it returns with a NULL pointer. Operator new never returns a NULL pointer but indicates the failure by throwing an exception instead. This is an advantage for the operator new since the computer programmer doesn’t have to check the returned pointer every time it calls new nor the computer programmer will miss such an exception.
Function malloc doesn’t construct an object (called constructor of object) but operator new does. Another advantage of new is that the operator can be overloaded while function malloc cannot. Operator new requires a specific number of objects to allocate while function malloc requires specifying the total number of bytes to allocate.
Memory can also be re-sized or reallocated by using function malloc by using realloc. However, operator new cannot perform this function and counts it as one of the operator’s main disadvantages. Another advantage of function malloc over operator new is that malloc can extend the amount of memory quickly. This is still using malloc and realloc. When doing the same situation in C++ language, the programmer has to create a new chunk of memory with operator new. The new memory has to have a modified size and will be copied over the data from the original buffer. The original buffer has to be deleted after the copy. This can be a slow process for a programmer.
Operator new has leverage over function malloc by returning the exact data type while the function returns void*. Function malloc also cannot be overloaded as compared to operator new wherein overloading is possible. Another feature of operator new is that there are less chances of making mistakes with the operator compared to function malloc. This is considered a plus for programmers especially if they are writing many codes or working on a code on a tight schedule. Function malloc and free can be used in C++, but it cannot be used directly. It is advisable to use new and delete instead.
Summary:
1. Malloc is a function while new is an operator.
2. New is a specific feature of the C++ language while malloc is used in the C-language.
3. Operator new follows with “delete” while function malloc goes with “free.”
4. Function malloc is library function for the C-language while operator new is a language-level construct from the C++ language.
- Differences Between Fraternity And Sorority - January 8, 2014
- Differences Between Lucite and Plastic - January 7, 2014
- Differences Between Oil and Butter - January 6, 2014
Search DifferenceBetween.net :
Email This Post : If you like this article or our site. Please spread the word. Share it with your friends/family.