I'm working on a C++ library, one of whose functions returns a (freshly allocated) pointer to an array of doubles. The API states that it is the responsibility of the caller to deallocate the memory.
However, that C++ library used to be implemented in C and the function in question allocates the memory with malloc()
. It also assumes that the caller will deallocate that memory with free()
.
Can I safely replace the call to malloc()
with a call to new
? Will the existing client code (that uses free()
break if I do so? All I could find so far was the official documentation of free()
, which states that
If ptr does not point to a block of memory allocated with [malloc, calloc or realloc], it causes undefined behavior.
But I believe this was written before C++ came along with its own allocation operators.