Difference Between Malloc and New

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.


IT Quiz

Test your knowledge about topics related to technology

1 / 10

With reference to a computer network, the exact meaning of the term VPN is

2 / 10

'.MOV' extension usually refers to what kind of file?

3 / 10

Which web browser is developed by the Google

4 / 10

Which two websites offer free e-mail services?

5 / 10

LED stands for:

6 / 10

What does the acronym RAM stand for?

7 / 10

Firewall in computer is used for

8 / 10

'IoT' refers to

9 / 10

Which of the following AI domain attempts to extract information from spoken and written words using algorithms?

10 / 10

The intention of Machine Learning is

Your score is


New/ delete should never be mixed with malloc/free. Unlike malloc, new does not need the size of the operator.

Key Takeaways

  1. Malloc and new are both used to allocate memory dynamically in C++ programs.
  2. Malloc is a C library function that returns a pointer to a block of memory of a specified size. At the same time, new is an operator in C++ that allocates memory and constructs an object of a specified type.
  3. New is safer than malloc because it ensures that the object’s constructor is called, which can prevent errors and memory leaks.

Malloc vs New

Malloc is a standard C function that can only be used in C++ and does not permit overriding legally. It stands for memory allocation and is used to allocate a block of memory. New is an operator that is only used in C++; it cannot be used in C and calculates the size of the memory with a compiler.

Malloc vs New

Want to save this article for later? Click the heart in the bottom right corner to save to your own articles box!

Malloc requires knowing the exact size of the operator to figure out what memory size it has to allot. Malloc is also incapable of making 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 Table

Parameters Of ComparisonMallocNew
Place in LanguageMalloc is more frequently used in C. Rarely, and it is used in C++.
New is only used in C++.
Returns It 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 a 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 for memory allocation. It is used to allocate a block of memory with a specific size dynamically. 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.

The 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 operator’s size to allocate the memory’s size.

It is not capable of calling the constructor of an object. A malloc function can be used to allocate memory for almost everything. The 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 an object’s constructor and can initialize memory. The constructor is called only after the memory has been allocated.

The compiler calculates the size of the memory. 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 the 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

  1. Malloc is a standard C function, whereas new is an operator.
  2. Malloc is mainly used in C whereas new is only used in C++. Malloc should only be used in C++ when it is necessary.
  3. When there is not enough memory, malloc returns to NULL while new throws up an exception.
  4. Always a malloc() should be matched with a free() and a new() with a delete. These two should not get interchanged
  5. Malloc returns to void while new returns to the proper type.
  6. Malloc allocates memory for almost anything and everything. New allocates memory for arrays, objects, and structs.
  7. The size has to be calculated manually for malloc, whereas in new, it is calculated automatically by the compiler.
  8. The Malloc function cannot call an object’s constructor, but a new operator can.
  9. Overriding is legally not allowed in malloc but is allowed in new.
  1. https://dl.acm.org/doi/abs/10.1145/1854273.1854303
  2. https://dl.acm.org/doi/abs/10.1145/2948618.2954331
One request?

I’ve put so much effort writing this blog post to provide value to you. It’ll be very helpful for me, if you consider sharing it on social media or with your friends/family. SHARING IS ♥️

Leave a Comment

Your email address will not be published. Required fields are marked *