C++ malloc() vs new

malloc() vs new

On this page we will discuss about difference between malloc() and operator new in C++.
In C++, malloc() is a function from the C standard library for dynamically allocating memory. It is used to allocate a block of memory on the heap.
In C++, the new operator is used to dynamically allocate memory and construct an object in that memory.

C++ malloc() vs new

In C++, malloc() and new are similar in that they both dynamically allocate memory on the heap, but new also constructs an object in that memory and has additional functionality.

Here we will discuss the key difference between malloc() and operator new.

Key differences between malloc() and new in C++

malloc() new
It is a function from the C standard library. It is a C++ operator.
It only allocates memory. It allocates memory and calls the constructor of the object being created.
It returns a pointer to the first byte of the allocated memory. It returns a pointer to the newly constructed object.
Memory allocated with malloc() must be explicitly freed using the free() function. Memory allocated with new is automatically freed when the program exits.
It returns void *. It returns exact data type.
It does not perform type checking. It checks for type correctness and throws an exception if the allocation fails.
It does not call constructors. It calls constructors.
malloc() cannot be overloaded. Operator new can be overloaded.
It can be used to allocate aligned memory. It cannot be used to allocate aligned memory.

Example of malloc()

Run
#include <iostream>
using namespace std;

int main()
{
  int *p = (int *)malloc(sizeof(int));	// Allocate memory for an integer
  if (p == NULL)
    {
      cout << "Memory allocation failed\n";
      return 1;
    }

  *p = 5;			// Assign a value to the memory
  cout << "The value of the integer is: " << *p << endl;

  free(p);			// Free the allocated memory

  return 0;
}

Output:

The value of the integer is: 5
In this example, we are using malloc() to dynamically allocate memory for an integer. malloc() does not initialize the memory so it can contain garbage values, also it does not call constructors for the object, it only allocates the raw memory.

Example of new

Run
#include <iostream>
using namespace std;

class Test
{
public:
  Test(int x)
  {
    value = x;
    cout << "Test object created with value " << value << endl;
  }
   ~Test()
  {
    cout << "Test object destroyed with value " << value << endl;
  }
  int getValue()
  {
    return value;
  }

private:
  int value;
};

int main()
{
  Test *p = new Test(5);  // Allocate memory for Test object and call constructor

  cout << "The value of the Test object is: " << p->getValue() << endl;

  delete p;	   // Free the allocated memory and call destructor

  return 0;
}

Output:

Test object created with value 5
The value of the Test object is: 5
Test object destroyed with value 5
In this example, we are using the new operator to dynamically allocate memory for an object of class Test. Here,the memory allocated by new will be freed automatically when the program exits, so you don’t have to worry about forgetting to call free().

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription