In the following program new
reuses a memory allocated by malloc
. But how to free the memory then? by free
or by delete
? how to call the destructor?
#include <iostream>
struct A
{
A() {}
~A() {}
};
int main()
{
void* p = malloc(sizeof(A));
A* pa = new (p) A();
// codes...
delete pa;
// pa ->~A();
// free(p);
}
Is it safe to reuse malloc
memory by new
? And how to free the memory?