I recently came across some C++ Code that is supposed to illustrate the many different types of errors that are possible to be caught in valgrind, gdb, insure, and such...
One of the examples is as follows:
// =============================================================================
// A base class without a virtual destructor
class Base
{
public:
~Base() { std::cout << "Base" << std::endl; }
};
// Derived should not be deleted through Base*
class Derived : public Base
{
public:
~Derived() { std::cout << "Derived" << std::endl; }
};
// A class that isn't Base
class NotBase
{
public:
~NotBase() { std::cout << "NotBase" << std::endl; }
};
// =============================================================================
// Wrong delete is called. Should call ~Base, then
// delete[] buf
void placement_new()
{
char* buf = new char[sizeof(Base)];
Base* p = new(buf) Base;
delete p;
}
My question pertains to the line:
Base* p = new(buf) Base;
I have never seen this syntax before and after much Googling I am not sure what to even search for in finding an explanation.
Can anyone point me in the right direction? Much apologies if this is redundant or simple, but I'm very curious what is going on in this example.
Thank you.