Applying delete
to a void *
pointer in C++ is illegal.
If your compiler supports this as a non-standard extension, then most likely delete
assumes that the unknown object pointed by that pointer has trivial destructor. In that case delete
does not have to do anything besides immediately handing over the control to the raw-memory deallocation function ::operator delete
, which probably just calls free
. (This last bit, of course, can depend on the implementation).
So, your question basically boils down to "how to determine the size of malloc
-ed memory block". There's no standard features that can do that. Either memorize the size yourself, when you allocate it. Or use the non-standard implementation-specific library features, if any such features are provided by your platform.
In some implementations this can be done through msize
function. But again, in order to do it meaningfully, you'd have to research your implementation first. You need to figure out how the memory is allocated by new
and/or what exactly delete v1
does, since it is not standard C++.