Tutorial Study Image

Malloc() Vs New in C++


November 8, 2022, Learn eTutorial
1279

In C++ programming, both the malloc() and the new operator are used to dynamically allocate the heap memory mainly for the variables as well as for the objects, but they do so in different ways. How do malloc and new vary from one another? In this tutorial let us discuss the main differences between the malloc and new

Differences between malloc and new

In C++, the same functionality is achieved by using both new and malloc(). They are used in order to allocate the memory at runtime. However, the syntax for malloc() and new differs. The primary distinction between new and malloc() is that the new is an operator, whereas the malloc() is a predefined standard library function that will be found in a stdlib header file.

Comparison based  on :

New

MALLOC()

Nature

it is an operator

it is a function

Language

The operator new is considered to be a unique feature for C++, Java, and C#.

The malloc() function is a C feature.

Constructor

The constructor of an object can be called using operator new.

Malloc() cannot make any calls to constructors.

sizeof()

new does not require the sizeof operator because it allocates enough memory for a specific type.

In order for malloc to determine how much memory to allocate, it needs the sizeof operator.

Initialization

 

 

While allocating memory to an object, the new operator can initialize the object.

Malloc could not perform memory initialization.

Overloading

 

Overloading operator new is possible.

Overloading malloc() is never permitted.

Deallocation

 

 

Memory was allocated with new and deallocated using "delete."

A free() function is mainly used to deallocate the memory allocated by malloc().

Reallocation

Memory is not reallocated by the new operator.

Memory that is allocated by the malloc() can be reallocated using the realloc().

Failure

Operator new throws an exception if it fails.

Malloc() will return a NULL if it fails.

 

Execution

The execution time is reduced by the new operator.

The execution time for malloc() is longer.

What is the definition of new as well as malloc?

new

Memory is allocated dynamically by the operator new. The new operator will allocate the heap memory and returns the initial address of that particular memory, that is assigned to a reference variable. Malloc() in C is comparable to the new operator. Although the malloc() function is compatible with the C++ compiler, it is preferable to use the new operator because it offers some benefits over the malloc ().

We know that the heap is limited in size and that the new operator will allocate memory to it. As a result, the new operator will fail if the heap runs out of memory and it tries to allocate memory. If the new operator fails to allocate memory, it will surely throw an exception, and if your program code is incapable of handling that exception, then the program will end unexpectedly. The delete operator can be used to release the memory that was allotted by the new operator. Since the new operator is an operator and not a function, it reduces the execution time.

To know more about the operator new in C++ check out the C++ memory management tutorial.

Malloc

To allocate the specified amount of memory to the heap, use the malloc() function. The method will be returning a pointer of type 'void,' which is then typecast to produce a pointer to the memory of a given type, which is then assigned to a reference variable. Since malloc() is used to allocate memory dynamically, it is comparable to the new operator in C++. Malloc() is a kind of standard library function.

Syntax 


type variable_name = (type *) malloc ( sizeof ( type ) );


In this case, "type" denotes the datatype of the variable for which memory must be allocated. The pointer returned by malloc() will be assigned to the reference variable with the name variable name. The (type*) specifies how to type cast to get a pointer to the memory of a particular type. The sizeof() function explains to malloc() how much memory is needed.
Because the pointer returned by the malloc() function is of void type, type casting is necessary in order to assign a type to the pointer. Because the function malloc() allocates raw memory, it is necessary to inform the function that it must allocate a certain amount of memory by using the sizeof() method. If the allocated memory is insufficient, realloc() can be used to enlarge or reallocate it.

Memory on the heap is allocated using the malloc() function. A NULL pointer is returned by the malloc() method if the heap runs out of memory. In order to avoid a system crash, the reference variable containing the pointer returned by malloc() should be verified before use.

free()  is used to deallocate the memory that the malloc() function was allocated. Malloc() takes longer to execute because function calls result in overhead.

The very important key differences between new and malloc to remember

  • The new operator is a C++-based concept that is also used in Java, C#, and other languages. In contrast, the standard library function malloc() is exclusively available in the C language and is supported by C++.
  • Because the new operator allocates enough memory for an object of a specified type, it doesn't require the sizing operator. However, in order for the malloc() function to know how much memory to allocate, it needs the sizeof() operator.
  • While an object is being declared, the new operator has the ability to call the object's constructor. On the other side, the constructor cannot be called by the malloc() function.
  • Malloc() could not be overloaded, but the operator 'new' could.
  • The code must handle the exception that the new operator throws if memory allocation fails; otherwise, the program will fail. The malloc() function, on the other hand, returns a NULL pointer if it is unable to allocate memory. The system will crash if the pointer is utilized without first verifying this.
  • 'delete' can be used to deallocate the memory that was allocated with the new operator. On the other hand, the free () function can deallocate memory that was allocated using the malloc() function.
  • The memory cannot be resized in any way once it has been allocated via a new operator. Memory allocated with the malloc() method, on the other hand, can be reallocated (resized) with the realloc() function.
  • Given that malloc() is a function and new is a construct, new takes less time to execute than malloc.

Conclusion

The malloc() function is the traditional method for dynamically allocating memory. These days, the new operator is utilized to allocate memory at runtime due to some advantages over malloc ().