This code passes in VS2010, but give run time error as Segmentation fault with g++ compiler.
I know we should not use delete
for an object which is instantiated by placement new
.
Please explain how it is working in VS2010.
If I use delete xp
or xp->~X()
(can delete one object only), the program runs successfully on both platforms.
Please provide a solution to delete an array of objects.
class X {
int i;
public:
X()
{
cout << "this = " << this << endl;
}
~X()
{
cout << "X::~X(): " << this << endl;
}
void* operator new(size_t, void* loc)
{
return loc;
}
};
int main()
{
int *l = new int[10];
cout << "l = " << l << endl;
X* xp = new (l) X[2]; // X at location l
delete[] xp;// passes in VS2010, but gives segmenatation fault with g++
}