Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is new?
3.
What is malloc()?
4.
malloc() vs new 
5.
Frequently Asked Questions
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

malloc() vs new

Introduction

Both malloc() and new in C++ are used for the same purpose. They are used for allocating memory on the runtime. But, malloc() and new have unique syntax. The principal distinction between the malloc() and new is that the new is an operator even as malloc() is a standard library function predefined in a stdlib header file.

Also, see Literals in C.Fibonacci Series in C++
 

What is new?

  • The new is a memory allocation operator used to allocate the memory on the runtime. The memory initialized through the new operator is allotted in a heap. It returns the beginning address of the memory, which is assigned to the variable. 
  • The functionality of the new operator is much like the malloc() characteristic, which turned into used withinside the C programming language.
  • C++ is well suited with the malloc() characteristic also. However, the new operator is generally used due to its advantages.

 

Syntax of new() operator:

type variable = new type(parameter_list); 

 

C++ code to represent the new operator:

#include <iostream>

using namespace std;
int main() {
  // integer type pointer variable declaration .
  int * p;
  // memory allocation to the pointer variable p.  
  p = new int;

  cout << "Enter the number : " << endl;
  cin >> * p;
  cout << "Entered number is " << * p << endl;
  return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Input:  

20

 

Output:

Enter the number :
Entered number is 20

You can try and compile with the help of online c++ compiler. You can also do a free certification of Basics of C++.

What is malloc()?

  • It is a function that allocates memory on the runtime. 
  • This function returns the void pointer, because of this void pointer, it can be assigned to any pointer type. 
  • This void pointer may be similarly typecast to get the pointer that points to the memory of a specific type. 

 

Syntax of malloc() operator:

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

 

C++ code to represent malloc():

#include <iostream>

#include<stdlib.h>

using namespace std;

int main() {

  int totalnum; // variable declaration  
  cout << "Enter the count of numbers :" << endl;
  cin >> totalnum;
  int * p; // declaration of pointer variable   
  p = (int * ) malloc(sizeof(int) * totalnum); // memory allocation to the poiner variable p 
  for (int i = 0; i < totalnum; i++) {
    cout << "Enter a number : " << endl;
    cin >> * (p + i);
  }
  cout << "Elements are : " << endl;
  for (int i = 0; i < totalnum; i++) {
    cout << * (p + i) << endl;
  }
  free(p);
  return 0;
} 
You can also try this code with Online C++ Compiler
Run Code

 

Input:

4
2
3
4
5

 

Output:

Enter the count of numbers :
Enter a number :
Enter a number :
Enter a number :
Enter a number :
Elements are :
2
3
4
5


You can also do a free certification of Basics of C++

malloc() vs new 

Also see, Abstract Data Types in C++

Frequently Asked Questions

  1. Does malloc call a constructor?
    Malloc does not call the constructor, unlike new and delete operators while an object is created.
     
  2. What will happen if you use malloc and free instead of delete?
    If we allocate memory with malloc usage, it ought to be deleted using free. If we allocate memory with the usage of new, it ought to be deleted by delete. When you call to delete a pointer, the compiler will automatically call the class's dtor for you. However, free won't.
     
  3. What happens if you don't deallocate memory?
    If you don’t deallocate memory, it will lead to a memory leak

Conclusion

This article discussed malloc(), new, their syntax, their definitions, concepts, a better understanding of the code example, and some differences between their functionalities related to each other.

See more, Difference Between C and Python

We hope that this blog has helped you and has enhanced your knowledge regarding Firewalls and if you would like to learn more, stay tuned for more blogs code studio. Do upvote our blog to help other ninjas grow. Happy Coding!"

Thank you for reading.

Live masterclass