????C++????????
???????????? ???????[ 2015/1/12 13:46:11 ] ????????C++ net
?????????????????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????????????????飬???????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11