malloc() vs new in C++
Both the malloc() and new in C++ are used for the same purpose. They are used for allocating memory at the runtime. But, malloc() and new have different syntax. The main difference between the malloc() and new is that the new is an operator while malloc() is a standard library function that is predefined in a stdlib header file.
What is new?
The new is a memory allocation operator, which is used to allocate the memory at the runtime. The memory initialized by the new operator is allocated in a heap. It returns the starting address of the memory, which gets assigned to the variable. The functionality of the new operator in C++ is similar to the malloc() function, which was used in the C programming language. C++ is compatible with the malloc() function also, but the new operator is mostly used because of its advantages.
Syntax of new operator
In the above syntax
type: It defines the datatype of the variable for which the memory is allocated by the new operator.
variable: It is the name of the variable that points to the memory.
parameter_list: It is the list of values that are initialized to a variable.
The new operator does not use the sizeof() operator to allocate the memory. It also does not use the resize as the new operator allocates sufficient memory for an object. It is a construct that calls the constructor at the time of declaration to initialize an object.
As we know that the new operator allocates the memory in a heap; if the memory is not available in a heap and the new operator tries to allocate the memory, then the exception is thrown. If our code is not able to handle the exception, then the program will be terminated abnormally.
Let's understand the new operator through an example.
What is malloc()?
A malloc() is a function that allocates memory at the runtime. This function returns the void pointer, which means that it can be assigned to any pointer type. This void pointer can be further typecast to get the pointer that points to the memory of a specified type.
The syntax of the malloc() function is given below:
type: it is the datatype of the variable for which the memory has to be allocated.
variable_name: It defines the name of the variable that points to the memory.
(type*): It is used for typecasting so that we can get the pointer of a specified type that points to the memory.
sizeof(): The sizeof() operator is used in the malloc() function to obtain the memory size required for the allocation.
Note: The malloc() function returns the void pointer, so typecasting is required to assign a different type to the pointer. The sizeof() operator is required in the malloc() function as the malloc() function returns the raw memory, so the sizeof() operator will tell the malloc() function how much memory is required for the allocation.
If the sufficient memory is not available, then the memory can be resized using realloc() function. As we know that all the dynamic memory requirements are fulfilled using heap memory, so malloc() function also allocates the memory in a heap and returns the pointer to it. The heap memory is very limited, so when our code starts execution, it marks the memory in use, and when our code completes its task, then it frees the memory by using the free() function. If the sufficient memory is not available, and our code tries to access the memory, then the malloc() function returns the NULL pointer. The memory which is allocated by the malloc() function can be deallocated by using the free() function.
Let's understand through an example.
If we do not use the free() function at the correct place, then it can lead to the cause of the dangling pointer. Let's understand this scenario through an example.
In the above code, we are calling the func() function. The func() function returns the integer pointer. Inside the func() function, we have declared a *p pointer, and the memory is allocated to this pointer variable using malloc() function. In this case, we are returning the pointer whose memory is already released. The ptr is a dangling pointer as it is pointing to the released memory location. Or we can say ptr is referring to that memory which is not pointed by the pointer.
Till now, we get to know about the new operator and the malloc() function. Now, we will see the differences between the new operator and the malloc() function.
Differences between the malloc() and new
Syntax of new operator
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.
In the above statements, we are declaring an integer pointer variable. The statement p = new int; allocates the memory space for an integer variable.
Syntax of malloc() is given below:
ptr: It is a pointer variable.
data_type: It can be any basic data type.
The above statement will allocate the memory for an integer variable in a heap, and then stores the address of the reserved memory in 'p' variable.