The new and malloc() both are used to dynamically allocate the memory. Though, new and malloc() are different in many contexts. The primary difference between new and malloc() is that new is the operator, used as a construct. On the other hand, the malloc() is a standard library function, used to allocate memory at runtime.
The other differences between them are discussed below in the comparison chart:
Content: new Vs malloc()
|Basis for Comparison||new||malloc( )|
|Language||The operator new is a specific feature of C++, Java, and C#.||The function malloc( ) is a feature of C.|
|Nature||"new" is an operator.||malloc( ) is a function.|
|sizeof( )||new doesn't need the sizeof operator asit allot enough memory for specific type||malloc requires the sizeof operator to know what memory size it has to allot.|
|Constructor||Operator new can call the constructor of an object.||malloc( ) can not at all make a call to a constructor.|
|Initialization||The operator new could initialize an object while allocating memory to it.||Memory initialization could not be done in malloc.|
|Overloading||Operator new can be overloaded.||The malloc( ) can never be overloaded.|
|Failure||On failure, operator new throws an exception.||On failure, malloc( ) returns a NULL.|
|Deallocation||The memory allocation by new, deallocated using "delete".||The memory allocation by malloc( ) is deallocated using a free( ) function.|
|Reallocation||The new operator does not reallocate memory.||Memory allocated by malloc() can be reallocated using realloc().|
|Execution||The operator new cuts the execution time.||The malloc( ) requires more time for execution.|
Definition of new
The operator new is a memory allocation operator that allocates memory dynamically. The new operator allocates memory in the heap and returns the starting address of that memory which is assigned to a reference variable. The new operator is similar to the malloc() in C. However, C++ compiler is compatible with malloc() but, it is best to use new operator as it has certain advantages over malloc(). The syntax of new operator is as follows:
type variable_name = new type(parameter_list);
Here, “type” denotes the datatype of the variable for which the memory has to be allocated. The word “variable_name” is the name given to the reference variable which holds the pointer to memory. The parenthesis here specifies calling of the constructor. The parameter_list is the list of the values that are passed to the constructor to initialize the newly constructed object.
The new operator allocates enough memory required for an object of a specific type. Hence, it doesn’t require a sizeof() operator nor does it require to resize the memory like malloc() which uses realloc() to reallocate the memory. The new operator is a construct; it calls the constructor of an object while declaration which is generally used to initialize the object.
We know that the new operator allocates the memory in the heap and the size of the heap is limited. So, if the heap is out of memory and new operator attempts to allocate the memory, it will lead to failure of the new operator. If the new operator fails to allocate the memory, it will throw an exception, and if your code is unable to handle that exception, the program terminates abnormally.
The memory allocated by the operator new can be freed using the delete operator. The new operator cuts off the execution time as it is an operator, not a function.
Definition of malloc()
The malloc() is a function which is used to allocate the requested amount of memory on the heap. The method returns the pointer of ‘void’ type which is further, type cast to get a pointer to a memory of a specified type and this pointer to memory is assigned to a reference variable. The malloc() function is similar to the new operator in C++ as it is used to allocate memory dynamically. The malloc() is a standard library function.
The syntax of the malloc() function is as follows:
type variable_name = (type *) malloc ( sizeof ( type ) );
Here, “type” indicates the datatype of the variable for which memory has to be allocated. The variable_name is the name of the reference variable to which the pointer returned by malloc() will be assigned. The (type*) describes the type casting to obtain a pointer to the memory in a specific type. The sizeof() describes malloc(), that what memory size is required.
The malloc() requires type casting because the pointer returned by the malloc() is of void type, so, to assign a type to the pointer, type casting is required. The sizeof() is required because the function malloc() allocates a raw memory hence, it is required to tell the malloc() function that what memory size it has to allocate. If the allocated memory is not sufficient, it can be resized or reallocate using realloc().
The malloc() function allocates memory on the heap. In case, the heap is out of memory then, the malloc() function returns a NULL pointer. Hence, the reference variable containing pointer returned by malloc(), should be checked before it is used, otherwise it may result in a system crash.
The memory allocated by the malloc() function is deallocated using free(). As function call leads to an overhead, malloc() requires more time for execution.
Key Differences Between new and malloc()
- The new operator is a construct introduced in C++ and used in Java, C#, etc. On the other hand malloc()is a standard library function found only in C language and supported by C++.
- The new operator allocates enough memory for the object of a specified type so, it does not require sizing operator. On the other hand, the malloc() function requires the sizeof() operator to let the function know that what memory size it has to allocate.
- The new operator can call the constructor of the object while declaration. On the other hand, the malloc() function can not call the constructor.
- The operator ‘new’ could be overloaded but malloc() couldn’t.
- If the new operator fails to allocate the memory, it throws an exception that must be handled by the code else the program will terminate. On the other hand, the malloc() function returns a NULL pointer if it fails to allocate memory. If the pointer is used without checking this, it will result in a system crash.
- The memory allocated using a new operator can be deallocated using ‘delete’. On the other hand, the memory allocated using malloc() function can be deallocated using free().
- Once the memory is allocated using a new operator, it can’t be resized in anyway. On the other hand, the memory allocated using malloc() function can be reallocated (resized) using realloc() function.
- The execution time of new is less as compared to malloc() because malloc is a function and new is a construct.
The malloc() function is the old way to dynamically allocate the memory. Nowadays, the new operator is used to allocate the memory at runtime because it has certain advantages over malloc().