malloc() vs new() in C/C++


malloc()

The function malloc() is used to allocate the requested size of bytes and it returns a pointer to the first byte of allocated memory. It returns null pointer, if fails.

Here is the syntax of malloc() in C++ language,

pointer_name = (cast-type*) malloc(size);

Here,

pointer_name − Any name given to the pointer.

cast-type − The datatype in which you want to cast the allocated memory by malloc().

size − Size of allocated memory in bytes.

Here is an example of malloc() in C language,

Example

 Live Demo

#include <stdio.h>
#include <stdlib.h>
int main() {
   int n = 4, i, *p, s = 0;
   p = (int*) malloc(n * sizeof(int));
   if(p == NULL) {
      printf("\nError! memory not allocated.");
      exit(0);
   }
   printf("\nEnter elements of array : ");
   for(i = 0; i < n; ++i) {
      scanf("%d", p + i);
      s += *(p + i);
   }
   printf("\nSum : %d", s);
   return 0;
}

Here is the output,

Enter elements of array : 32 23 21 8
Sum : 84

In the above program, four variables are declared and one of them is a pointer variable *p which is storing the memory allocated by malloc. We are printing the sum of elements.

int n = 4, i, *p, s = 0;
p = (int*) malloc(n * sizeof(int));
if(p == NULL) {
   printf("\nError! memory not allocated.");
   exit(0);
}
printf("\nEnter elements of array : ");
for(i = 0; i < n; ++i) {
   scanf("%d", p + i);
   s += *(p + i);
}
printf("\nSum : %d", s);

new()

The new operator requests for the memory allocation in heap. If the sufficient memory is available, it initializes the memory to the pointer variable and returns its address.

Here is the syntax of new operator in C++ language,

pointer_variable = new datatype;

Here is the syntax to initialize the memory,

pointer_variable = new datatype(value);

Here is the syntax to allocate a block of memory,

pointer_variable = new datatype[size];

Here is an example of new operator in C++ language,

Example

 Live Demo

#include <iostream>
using namespace std;
int main () {
   int *ptr1 = NULL;
   ptr1 = new int;
   float *ptr2 = new float(223.324);
   int *ptr3 = new int[28];
   *ptr1 = 28;
   cout << "Value of pointer variable 1 : " << *ptr1 << endl;
   cout << "Value of pointer variable 2 : " << *ptr2 << endl;
   if (!ptr3)
   cout << "Allocation of memory failed\n";
   else {
      for (int i = 10; i < 15; i++)
      ptr3[i] = i+1;

      cout << "Value to store in block of memory: ";
      for (int i = 10; i < 15; i++)
      cout << ptr3[i] << " ";
   }
   return 0;
}

Output

Value of pointer variable 1 : 28
Value of pointer variable 2 : 223.324
Value to store in block of memory: 11 12 13 14 15

In the above program, three pointer variables are declared as ptr1, ptr2 and ptr3. The pointer variables ptr1 and ptr2 are initialized with the value using new() and ptr3 is storing the allocated block of memory by new() function.

ptr1 = new int;
float *ptr2 = new float(223.324);
int *ptr3 = new int[28];
*ptr1 = 28;

Updated on: 26-Jun-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements