You never need to use malloc
in C ++.
Well, now, as I already said, the exception is when you use C code, which for some reason takes responsibility for the memory block that you give it, and then calls the free
pointer to this memory to free it.
I have never seen this before (I usually do not use C libraries, and I donβt know how common this script is), itβs just a far-fetched situation where I can think about where using malloc
will not be optional, because this undefined behavior causes free
on a piece of memory created by new
.
source share