The main difference is that malloc() and free() don't know anything about constructors and
destructors, where as new and delete do. The following lists the main differences:
new automatically computes the size of the data object. In malloc you would have to use the
sizeof operator.
new automatically returns the correct pointer type. In malloc you would have to use a type
cast.
with new you can initialize the object while creating the object.
new and delete can be overloaded.
It's safe to delete a NULL pointer, but you'll get core dump to free a NULL pointer.
destructors, where as new and delete do. The following lists the main differences:
new automatically computes the size of the data object. In malloc you would have to use the
sizeof operator.
new automatically returns the correct pointer type. In malloc you would have to use a type
cast.
with new you can initialize the object while creating the object.
new and delete can be overloaded.
It's safe to delete a NULL pointer, but you'll get core dump to free a NULL pointer.
Comments
Post a Comment
https://gengwg.blogspot.com/