I see in C++ there are multiple ways to allocate and free data and I understand that when you call malloc
you should call free
and when you use the
There are a few things which new
does that malloc
doesn’t:
new
constructs the object by calling the constructor of that objectnew
doesn’t require typecasting of allocated memory.So, if you use malloc
, then you need to do above things explicitly, which is not always practical. Additionally, new
can be overloaded but malloc
can’t be.