M
mail.dsp
Suppose in a class we overload four operators:
operator new
operator delete
operator new[]
operator delete[]
class Test{
public:
void * operator new (size_t t){
cout<<"\nCalling... new";
return malloc(t);
}
void operator delete (void *p){
cout<<"\nCalling ... delete";
free(p);
}
void * operator new [] (size_t t){
cout<<"\nCalling ... new[]";
return malloc(t);
}
void operator delete [] (void *p){
cout<<"\nCalling ... delete[]";
free(p);
}
};
If we perform
Test *p=0; delete p;
It calls operator delete.
But if we perform
Test *p=0; delete []p;
It doesn't call operator delete[] until and unless we don't call
operator new[]. Means If we do like this;
Test *p=0; p=new Test[10]; delete[] p; It calls operator delete[].
Why???
I've executed this code on g++ 4.1.2 20070925.
Thanks in advance
operator new
operator delete
operator new[]
operator delete[]
class Test{
public:
void * operator new (size_t t){
cout<<"\nCalling... new";
return malloc(t);
}
void operator delete (void *p){
cout<<"\nCalling ... delete";
free(p);
}
void * operator new [] (size_t t){
cout<<"\nCalling ... new[]";
return malloc(t);
}
void operator delete [] (void *p){
cout<<"\nCalling ... delete[]";
free(p);
}
};
If we perform
Test *p=0; delete p;
It calls operator delete.
But if we perform
Test *p=0; delete []p;
It doesn't call operator delete[] until and unless we don't call
operator new[]. Means If we do like this;
Test *p=0; p=new Test[10]; delete[] p; It calls operator delete[].
Why???
I've executed this code on g++ 4.1.2 20070925.
Thanks in advance