?????????????????C++?????????????????????????new ?? delete ????????????
???????з????ζ???????????????C++????????????????????????????????????????????????????????????????????????????????檔
???????????????飬?????????????????ó????????????new ?? delete???????????????C++?????е???????????????????????У?????????????????????????????С???????????????????????????????
?????????棬?????????????????????????е??????
?????????????????new ??delete??????ò?????C++?е?????????????????????new ??delete??????????????????????????????????????new ??delete??
???????????????????÷?????????????С???????з???????????????????????new ??delete??????????????
????????????new ??delete ??????
??????????????????new ?? delete ???????????????:

 

void * operator new(size_t size)
{
void *p = malloc(size);
return (p);
}
void operator delete(void *p);
{
free(p);
}

??????δ??????????????????????????????????????????C++?????????????????????malloc() ??free()??
????????????????new ?? delete ????????????????????????????????????

 

class TestClass {
public:
void * operator new(size_t size);
void operator delete(void *p);
// .. other members here ...
};
void *TestClass::operator new(size_t size)
{
void *p = malloc(size); // Replace this with alternative allocator
return (p);
}
void TestClass::operator delete(void *p)
{
free(p); // Replace this with alternative de-allocator
}

????????TestClass ?????????????????δ?????????????κδ?TestClass ??е??????????????????????????????????new ?? delete ???????????????new ?? delete ?????????????????????????ò??????????????????????з???????????
????????????????? new[ ] ?? delete[ ]
????????С?????????????????????????????????????new ?? delete ??????????????????????????????????new[ ]??delete[ ] ????????????Щ??????????????
????C++????????????????????????????????????????????????????????????????????????????????new[ ] ?? delete[ ]????????

 

class TestClass {
public:
void * operator new[ ](size_t size);
void operator delete[ ](void *p);
// .. other members here ..
};
void *TestClass::operator new[ ](size_t size)
{
void *p = malloc(size);
return (p);
}
void TestClass::operator delete[ ](void *p)
{
free(p);
}
int main(void)
{
TestClass *p = new TestClass[10];
// ... etc ...
delete[ ] p;
}

??????????????????C++??????new[]???????е????????????????С????????洢??????????Щ???????????????????????????????????t????????????????飬???????????????????