The main differences between new and malloc() are:
newis type-safe and automatically calculates the required memory size based on the object type, whilemalloc()is not type-safe and requires manual size specification in bytes.newcalls the constructor of the object being created, ensuring proper initialization, whereasmalloc()does not invoke any constructor.newreturns a pointer to the type being allocated, whilemalloc()returns avoid*pointer that requires a typecast.newthrows astd::bad_allocexception if memory allocation fails, whilemalloc()returns aNULLpointer for error checking.newis preferred in modern C++ programming, butmalloc()may be necessary when working with legacy code or interfacing with C libraries.
Riya Answered question July 6, 2023
