Difference Between Malloc and New

June 2022 · 5 minute read

Malloc is a feature in the C language whereas new is a fundamental feature in C++. Malloc is essentially a standard function while new is an operator. Malloc should not be used in C++ without an essential reason. New/ delete should never be mixed with malloc/free. Unlike malloc, new does not need the size of the operator.

Malloc vs New

The main difference between malloc() and new() is that the former one is a standard C function and the latter is an operator which can only be used in C++. Malloc can and should only be used in C++ when there is some good reason to. New allows overriding but malloc does not permit it legally.

Malloc requires knowing the exact size of the operator to figure out what memory size it has to allot. Malloc is also incapable to make a call to the constructor. It returns to NULL when it senses that there is some shortage of memory.

The new operator does not need any prior information regarding the exact size of the operator to assign a place for memory. The new operator also can call the constructor of an object. It is known to bring up an exception when there is a shortage of memory.

Comparison Between Malloc And New In Tabular Form

Parameters Of ComparisonMallocNew
Place in LanguageMalloc is more frequently used in C. Rarely it is used in C++.
New is only used in C++.
ReturnsIt returns to void.
New returns to the proper type.
SizeIn malloc, the size has to be calculated manually
In New the required size of the compiler is calculated by a compiler
OverridingThe malloc function cannot be overridden legally.
The new operator provides the opportunity to override.
TypeMalloc is standard C function.
It uses operators like ==,+= etc .
Behavior when there is not enough memoryMalloc returns to NULL when there is a shortage of memory.
New brings up an exception during a shortage of memory.
DeallocatingA malloc() should be matched with a free().
A new() should be matched with a delete().
Allocates memory forAlmost everything.
Only for arrays, objects, and structs.

What is Malloc?

Malloc() is a standard library function in C which stands for memory allocation. It is used to dynamically allocate a block of memory with a specific size. The size of the memory is allocated in bytes. It usually returns a void type of pointer since it carries some garbage value. It does not initialize memory at the time of execution. Malloc function should only be used in C++ when it is very much necessary to use, otherwise, its usage should be restricted to only C. When malloc senses a shortage of memory it immediately returns to NULL. It does not perform memory initialization. It contains 2 arguments. A malloc() has to be always matched with a free(). The size of memory in malloc has to be calculated manually. It requires prior knowledge about the size of the operator to allocate the size of the memory. It is not capable of calling the constructor of an object. A malloc function can be used to allocate memory for almost everything. Malloc function does not allow overriding legally. A malloc works slower than a new operator in C++ because an operator is always faster than a function.

Syntax:- ptr = (castType*) malloc(size);

What is New?

New is an operator in C++ which cannot be used in C. The new operator can call a constructor of an object and can initialize memory. The constructor is called only after the memory has been allocated. The size of the memory is calculated by the compiler. It returns to the proper type. It can use operators like ==, += etc. The new operator only allocates memory for arrays, objects, and structs. In C++, the new operator is applied to put forward a request for allocation of memory on the heap. Provided that enough memory is available, the new operator initializes it and thereafter works to return the newly allocated and formed memory to the pointer variable. A new() should always be matched with a delete(). This operator is used for dynamic memory allocation, object construction, and destruction. The memory is allocated for objects from a pool known as the free store. It works much faster than malloc since it is an operator and not a function. 

Syntax:- pointer-variable = new data-type;

Main Differences Between Malloc and New

  • Malloc is a standard C function whereas new is an operator.
  • Malloc is mainly used in C whereas new is only used in C++. Malloc should only be used in C++ when it is necessary.
  • When there is not enough memory, malloc returns to NULL while new throws up an exception.
  • Always a malloc() should be matched with a free() and a new() with a delete. These two should not get interchanged
  • Malloc returns to void while new returns to the proper type.
  • Malloc allocates memory for almost anything and everything. New allocates memory for arrays, objects, and structs.
  • The size has to be calculated manually for malloc whereas in new it is calculated automatically by the compiler.
  • Malloc function cannot call the constructor of an object but a new operator can.
  • Overriding is legally not allowed in malloc but is allowed in new.
  • Conclusion

    Malloc() is a standard function in C which is also used in C++ when necessary. New() is an operator whose usage is restricted to C++ only. Malloc() has to match with a free() and new() with a delete() to make it work properly. The two should never get interchanged. A new is much more advanced than a malloc as it requires no prior knowledge of the size of the operator to allocate space for memory but a malloc function requires it. A malloc function cannot call the constructor of an object a new operator is capable of doing so. A malloc function is known to allocate memory for almost anything but the new operator allocates memory for only arrays, objects, and structs in C++. Although a malloc is much faster than new on average the speed may vary depending on the individual invocation. When there is a shortage of memory, malloc returns to NULL whereas new throws up an exception on error.

    References

  • https://dl.acm.org/doi/abs/10.1145/1854273.1854303
  • https://dl.acm.org/doi/abs/10.1145/2948618.2954331
  • ncG1vNJzZmiZo6Cur8XDop2fnaKau6SxjZympmeUnrOnsdGepZydXZeytcPEnqVmpZGhubCvjJqlnWWemsRuw8itn2askZe5pns%3D