Both malloc() and new serve the same purpose: they are used to allocate memory at runtime. However, malloc() and new differ in syntax. malloc() is a standard library function defined in the stdlib header file, whereas new is an operator.
What is new?
The new operator is used for memory allocation at runtime. The memory allocated by the new operator resides in the heap. It returns the starting address of the memory and assigns it to a variable. The functionality of the new operator in C++ is similar to that of the malloc() function used in C. C++ is also compatible with the malloc() function, but the new operator is generally preferred due to its advantages.
Syntax of the new operator
Type variable_name = new Type(parameter_list);
In the above syntax,
Type: Defines the data type of the variable for which memory is allocated using the new operator.
Variable Name: The name of the variable that points to the memory.
Parameter List: A list of values to initialize the variable.
The new operator does not use the sizeof() operator to allocate memory. It also does not use resize because the new operator allocates enough memory for the object. It is a constructor that calls the constructor function to initialize the object at declaration.
Since the new operator allocates memory in the heap, if there is not enough memory available in the heap when the new operator tries to allocate memory, it will throw an exception. If our code cannot handle the exception, the program will terminate abnormally.
Let’s understand the new operator through an example.
#include <iostream>
using namespace std;
int main() {
int *ptr; // Declaration of integer pointer variable
ptr = new int; // Allocate memory for pointer variable
cout << "Please enter a number:" << endl;
cin >> *ptr;
cout << "The entered number is:" << *ptr << endl;
return 0;
}
Output:
👇 Click to Claim 👇
👉 Collection of C Language Knowledge Materials
What is malloc()?
The malloc() function is used to allocate memory at runtime. This function returns a void pointer, which means it can be allocated to any pointer type. This void pointer can further be typecast to obtain a pointer to specific type memory.
Syntax of the malloc() function:
Type *variable_name = (Type *)malloc(sizeof(Type));
Where,
Type: The data type of the variable for which memory is allocated.
Variable Name: The name of the variable that points to the memory.
(Type*): Used for typecasting to obtain a pointer to specific type memory.
sizeof(): The sizeof() operator is used to get the size of memory required for allocation.
Note: The malloc() function returns a void pointer, so typecasting is required to allocate different types of pointers. The malloc() function requires the sizeof() operator because it returns raw memory, and the sizeof() operator tells the malloc() function how much memory to allocate.
If there is not enough memory in the heap, the realloc() function can be used to resize it. Since all dynamic memory needs are met using heap memory, the malloc() function also allocates memory in the heap and returns a pointer to the heap memory. Heap memory is very limited, so when our code starts executing, it marks the memory used, and when the code completes its task, it uses the free() function to release the memory. If there is not enough memory in the heap and our code tries to access it, the malloc() function will return a NULL pointer. Memory allocated by the malloc() function can be released using the free() function.
Let’s understand through an example.
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
int len; // Variable declaration
std::cout << "Enter the number of digits:" << std::endl;
std::cin >> len;
int *ptr; // Declaration of pointer variable
ptr = (int *)malloc(sizeof(int) * len); // Allocate memory for pointer variable
for (int i = 0; i < len; i++) {
std::cout << "Enter a number:" << std::endl;
std::cin >> *(ptr + i);
}
std::cout << "The entered elements are:" << std::endl;
for (int i = 0; i < len; i++) {
std::cout << *(ptr + i) << std::endl;
}
free(ptr);
return 0;
}
Output Result:
If we do not use the free() function at the right place, it can lead to dangling pointer issues. Let’s understand this situation through an example.
#include <iostream>
#include <stdlib.h>
using namespace std;
int *func() {
int *p;
p = (int *)malloc(sizeof(int));
free(p);
return p;
}
int main() {
int *ptr;
ptr = func();
free(ptr);
return 0;
}
In the above code, we called the func() function. The func() function returns an integer pointer. Inside the func() function, we declared a pointer p and used the malloc() function to allocate memory for this pointer variable. In this case, we returned a pointer to memory that has been freed. ptr is a dangling pointer because it points to a memory location that has been freed. Alternatively, we can say that ptr points to memory that is not referenced by any pointer.
So far, we have learned about the new operator and the malloc() function. Now, we will see the differences between the new operator and the malloc() function. The differences between malloc() and new are that the new operator constructs an object, i.e., it calls the constructor to initialize the object, whereas the malloc() function does not call the constructor. The new operator calls the constructor, while the delete operator calls the destructor to destroy the object.
The biggest difference between malloc() and new.
-
The new operator is used to construct an object, which means it calls the constructor to initialize the object, while the malloc() function does not call the constructor. The new operator calls the constructor, and the delete operator calls the destructor to destroy the object. This is the biggest difference between malloc() and new.
-
The new operator is an operator, while malloc() is a predefined function in the stdlib header file.
-
The new operator can be overloaded, while the malloc() function cannot be overloaded.
-
If there is not enough memory available in the heap, the new operator will throw an exception, while the malloc() function will return a null pointer (NULL).
-
When using the new operator, we need to specify the number of objects to allocate, whereas in the malloc() function, we need to specify the number of bytes to allocate.
-
For the new operator, we need to use the delete operator to free memory. For the malloc() function, we need to use the free() function to free memory.
The syntax of the new operator is as follows:
type reference_variable = new type_name;
Where,
-
type: Defines the data type of the reference variable.
-
reference_variable: The name of the pointer variable.
-
new: The operator used to allocate memory.
-
type name: Can be any basic data type.
For example:
int *p;
p = new int;
In the above statement, we declared an integer pointer variable. The statement p = new int; allocates memory space for an integer variable.
Syntax of malloc() is as follows:
int *ptr = (data_type*) malloc(sizeof(data_type));
Where,
-
ptr: Pointer variable.
-
data_type: Can be any basic data type.
For example:
int *p;
p = (int *) malloc(sizeof(int));
-
The above statement will allocate memory for an integer variable in the heap and store the reserved memory address in the variable ‘p’.
-
On the other hand, memory allocated using the malloc() function can be released using the free() function.
-
Once memory is allocated using the new operator, it cannot be resized. However, memory allocated using the malloc() function can be reallocated using the realloc() function. The execution time of the new operator is less than that of the malloc() function because new is a constructor, while malloc is a function.
-
The new operator does not return a separate pointer variable; it returns the address of the newly created object.
-
On the other hand, the malloc() function returns a void pointer, which can further be typecast to the specified type.
Popular Recommendations
-
CLion Tutorial – Increase IDE Heap Size in CLion
-
C Language Algorithm – “Text Left Alignment” Algorithm Problem
-
C++ Tutorial – Detailed Explanation of Memory Management in C++