Would there ever be a reason to initialize a pointer in a constructor with malloc?
At least couple of reasons comes to my mind:
There could be more, but you need to be careful when working with raw pointers, either initialized by new
or malloc()
. For example your class violates rule of 3/5/0. Best way to handle that - use a smart pointer.
Also you need to remember that with malloc()
you need to be sure that memory is properly initialized, it can be done with memset()
or simple assignments with POD types or (and this is mandatory for non POD) through placement new. That usage is not trivial so you would want to deal with that when you really need it.