malloc() Function vs new Operator in C++

Share Your Love

The malloc function and new operator both are working for creating the memory blocks dynamically. 

The declaration of the malloc() function and a new operator is different but we can use both in C++. 

The malloc function comes under the header file called stdlib.h whereas the new operator is overloaded by the constructor to create the memory dynamically. 

What is new?

The new operator is allocated the memory dynamically at runtime. These memory blocks are generally created at the Heap Memory

The initial address is returned by the new operator allocated memory. 

The malloc function and new working principle allocated memory dynamically at a heap.  

In C programming we mostly use the malloc() function and in C++ we use a new operator to perform this task.

Syntax of a new operator:

type variable = new type(parameter_list);  

In the above syntax

type: Here type any data type, 

variable: and variable is a pointer variable.

new: the new operator followed by type that is any data type.

parameter_list: with parameter contains data.

The new operator does not use the sizeof() operator which is generally used in malloc() and calloc() functions in C. Does not resize the memory blocks like in C. 

It allocates the memory blocks at runtime once with initialising the data at the time of constructor call.

The new operator for the malloc() function allocates the memory in the Heap Memory structure. If there are not sufficient memory blocks available then it tries to find empty spaces on the heap but if it fails then it throws an exception in C++.

Let’s understand the use of a new operator through an example:

#include <iostream>  
using namespace std;  
int main()  
 int *ptr = new int;  // integer pointer variable declaration allocating memory to the pointer variable ptr.  
 std::cout << "Enter the number : " << std::endl;  
 std::cin >>*ptr;  
 std::cout << "Entered number is " <<*ptr<< std::endl;  
return 0;  


Enter the number: 25
Entered number is 25

Now we start discussing the malloc() function and how it declares the data objects allocate the memory.

What is malloc()?

The malloc function allocates the memory  at runtime with the total number of size of the data objects.

The pointer variable points to the memory location which is generally created by the malloc function and returns to the main memory with initial address.

This pointer returns to the void pointer type Which means that it can be assigned to any type. Then void pointer typecast to a particular type returned by the pointer object.

The syntax of the malloc() function is given below:

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


type: means data-type of a particular variable of which memory blocks are allocated.

variable_name: It is a pointer variable that points to the specific memory block.

(type*):  It is a type of pointer variable that specified type that points to a specific memory location.

sizeof(): This returns the size of a particular object for use of memory allocation.

If adequate memory is not available, the realloc() function can be used to resize the memory. Because heap memory is used to satisfy all dynamic memory requirements, the malloc() method allocates memory in a heap and returns a pointer to it.

Because heap memory is limited, our code identifies the memory as in use when it begins execution, and when it completes its purpose, it frees the memory using the free() function. If there isn’t enough memory available and our code tries to use it, the malloc() function produces a NULL pointer.

The free() function can also be used to deallocate memory allocated by the malloc() method.

Let’s understand through an example:

#include <iostream>  
using namespace std;  
int main()  
  int len;   // variable declaration  
  std::cout << "Enter the count of numbers :" << std::endl;  
  std::cin >> len;  
  int *ptr; // pointer variable declaration  
  ptr=(int*) malloc(len*sizeof(int));  // allocating memory to  the poiner variable  
  for(int i=0;i<len;i++)  
      std::cout << "Enter a number : " << std::endl;  
      std::cin >> *(ptr+i);  
  std::cout << "Entered elements are : " << std::endl;  
  for(int i=0;i<len;i++)  
    std::cout << *(ptr+i) << std::endl;  
  return 0;  

If the free() function is not used correctly, it can result in a dangling pointer.

Let’s understand this scenario through an example.

#include <iostream>  
#include <stdlib.h>  
using namespace std;  
int *func()  
    int *p;  
    p=(int*) malloc(sizeof(int));  
    return p;  
int main()  
 int *ptr;  
 return 0;  

In the above program, we are calling a func() function.

Now the func() function returns the integer pointer.

Inside the func() function, here we declared *p and allocate memory dynamically using the malloc() function.

After that, we release the pointer then returns to the main function. So here pointer p doesn’t hold any memory location that’s why called as dangling pointer. Because the pointer did not point to any memory locations.

In the end, we know about the new and malloc() function. Now we look at the difference between the new operator and malloc() function.

Differences between the malloc() and new

  • The new operator creates an object by invoking the function constructor, but the malloc() method does not invoke the constructor[function Object() { [native code] }]. The constructor is called by the new operator, while the destructor is called by the delete operator to destroy the object. This is the most significant distinction between malloc() and new.
  • The new is an operator while malloc() function is a predefined function inside stdlib.h header file.
  • The new operator overloaded while malloc() function didn’t overloaded.
  • If there isn’t enough memory in a heap, the new operator will throw an exception, and the malloc() function will return a NULL pointer.
  • We must indicate the number of objects to be allocated in the new operator, whereas we must specify the amount of bytes to be allocated in the malloc() function.
  • To deallocate memory in the context of a new operator, we must use the delete operator. However, in the case of the malloc() function, we must deallocate the memory using the free() function.

Syntax Of new Operator:

type reference_variable = new type name;


type: It defines the data type of the reference variable.

reference_variable: It is the name of the pointer variable.

new: It is an operator used for allocating the memory.

type name: It can be any basic data type.

For example,

int *p = new int;   

We are declaring an integer pointer variable in the above statements. The statement 

p = new int; creates a new integer variable in memory.

Syntax of malloc() is given below:

(type) *ptr = (data_type*) malloc(sizeof(data_type));  

ptr: It is a pointer variable.

data_type: It is a basic data type.

For example,

int *p;
p = (int *) malloc(sizeof(int));

The above statement allocates memory on a heap for an integer variable, then stores the address of the reserved memory in the ‘p’ variable.

  • The memory allocated with the malloc() method, on the other hand, can be freed with the free() function.
  • The memory can’t be resized once it’s been allocated with the new operator. The memory, on the other hand, is allocated using the malloc() method and then reallocated using the realloc() function.
  • Because new is a constructor and malloc() is a function, new takes less time to execute than malloc().
  • The address of the newly created object is returned by the new operator, not the distinct pointer variable. The malloc() function, on the other hand, returns a void pointer that can be typecast into any type.

Please comment and share this article if you find any good for you, and contribute the article WhatsApp us.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 429

Newsletter Updates

Enter your email address below to subscribe to our newsletter