What is the Difference Between malloc , calloc , new
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

gurpreetsConnect With a Mentor Commented:
The  malloc()function provides a simple,
general-purpose memory allocation package. The  malloc() function returns a pointer to a block of specified bytes suitably aligned for any use.

The calloc()function allocates space for an array of specified elements of specified size. The space is initialized to zeros.

The new() function does two things. It first allocates raw memory by calling malloc and then calls the constructor of the object.
gurpreets missed at least four very important differences:

First, new is an operator and not a function.

Second, memory allocated with new must be freed with delete.

Third, new is for C++ and can't be used in C, while malloc() and calloc() can be used in C or C++.

Fourth, new reports errors in a very different way than malloc() or calloc().

..B ekiM
Two more things about new:
1. You don't need to calculate the number of bytes to allocate - new does the math for you.
2. It's type safe - the returned pointer type matches the type of the allocated object, not void*.
All Courses

From novice to tech pro — start learning today.