estadisticas web Skip to content

Difference between new and malloc ()

Difference between new and malloc ()

The new and malloc () are both used to dynamically allocate memory. Although, new and malloc () are different in many contexts. The main difference between new and malloc () which new the operator, used as a construct. On the other hand, malloc () a standard library function, used to allocate memory at runtime. The other differences between them are discussed below in the comparison table:

Comparative chart

Basis for comparing newalloc ()
language The new operator has a specific functionality of C ++, Java and C #. The malloc () function is a functionality of C.
Nature "new" an operator. malloc () a function.
size of( ) new does not need the sizeof operator to allocate enough memory for a specific type malloc requires the sizeof operator to know which memory size it should allocate.
Manufacturer The new operator can call the constructor of an object. malloc () cannot call the constructor at all.
Initialization The new operator may initialize an object while allocating memory to it. Memory initialization cannot be done in malloc.
Overload The new operator can be overloaded. The malloc () can never be overloaded.
Failure In case of failure, the new operator throws an exception. If an error occurs, malloc () returns a NULL value.
deallocation Memory allocation by new, deallocated using "clear". Memory allocation by malloc () is deallocated using a free () function.
Reallocation The new operator does not reallocate memory. The memory allocated by malloc () can be reallocated using realloc ().
execution The new operator cuts the run time. Malloc () takes longer to run.

Definition again

The operator new a memory allocation operator that dynamically allocates memory. The new operator allocates memory to the heap and returns the starting address of that memory which is assigned to a reference variable. The new operator similar to malloc () in C. However, the C ++ compiler compatible with malloc (), but better to use the new operator as it has certain advantages over malloc (). The syntax of the new operator is as follows:

 tipo variable_name = new type (parameter_list); 

Here, "type" indicates the data type of the variable for which memory is to be allocated. The word "variable_name" is the name given to the reference variable that contains the memory pointer. The parenthesis here specifies the constructor call. Parameter_list the list of values ​​that are passed to the constructor to initialize the newly constructed object.

The new operator allocates enough memory needed for an object of a specific type. So, it doesn't require a sizeof () operator nor does it require resizing memory like malloc () which uses realloc () to reallocate memory. The new operator is a construct; calls the constructor of an object while the declaration is generally used to initialize the object.

We know that the new operator allocates memory in the heap and the limited heap size. So if the heap has run out of memory and the new operator tries to allocate memory, it will lead to the failure of the new operator. If the new operator fails to allocate memory, it will throw an exception and, if the code cannot handle this exception, the program ends abnormally.

Memory allocated by the new operator can be freed using the delete operator. The new operator interrupts the run time as an operator, not a function.

Definition of malloc ()

Malloc () a function that is used to allocate the amount of memory required in the heap. The method returns the pointer of type 'void' which further, type cast to get a pointer to a memory of a specified type and this pointer to memory is assigned to a reference variable. The malloc () function similar to the new operator in C ++ as it is used to allocate memory dynamically. Malloc () a standard library function. The syntax of the malloc () function is the following:

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

Here, "type" indicates the data type of the variable for which memory is to be allocated. Variable_name the name of the reference variable to which the pointer returned by malloc () will be assigned. The (type *) describes the type of casting to obtain a memory pointer in a specific type. The sizeof () describes malloc (), which requires memory size.

Malloc () requires type casting because the pointer returned by malloc () of type void, therefore, to assign a type to the pointer, you need to type cast. The size of () required because the malloc () function allocates raw memory, so you need to tell the malloc () function what memory size it should allocate. If there is not enough allocated memory, it can be resized or reallocated using realloc ().

The malloc () function allocates memory on the heap. In case the heap has run out of memory, the malloc () function returns a NULL pointer. Therefore, the reference variable containing the pointer returned by malloc (), must be checked before being used, otherwise a system crash could occur.

The memory allocated by the malloc () function is deallocated using free (). Since calling the function leads to an overload, malloc () takes longer to run.

Key differences between new and malloc ()

  1. The new operator is a construct introduced in C ++ and used in Java, C #, etc. On the other hand, malloc () a standard library function available only in C language and supported by C ++.
  2. The new operator allocates enough memory for the object of a specified type, so it does not require the sizing operator. On the other hand, the malloc () function requires the sizeof () operator to let the function know what memory size it should allocate.
  3. The new operator can call the object constructor during the declaration. On the other hand, the malloc () function cannot call the constructor.
  4. The 'new' operator may be overloaded but malloc () cannot.
  5. If the new operator fails to allocate memory, it throws an exception which must be handled by the code otherwise the program will be terminated. On the other hand, the malloc () function returns a NULL pointer if it cannot allocate memory. If the pointer is used without checking it, a system crash will occur.
  6. Memory allocated using a new operator can be deallocated using 'clear'. On the other hand, memory allocated using the malloc () function can be deallocated using free ().
  7. Once the memory is assigned using a new operator, it cannot be resized in any way. On the other hand, memory allocated using the malloc () function can be reallocated (resized) using the realloc () function.
  8. The execution time of new is shorter than malloc () because malloc is a function and new is a construct.

Conclusion:

The malloc () function is the old way of dynamically allocating memory. Nowadays, the new operator is used to allocate memory at runtime because it has some advantages over malloc ().