??C++?е?new??delete
???????????? ???????[ 2014/3/5 10:38:23 ] ????????C++ ??????? ????
????(2) delete opeartor
????[::] delete cast-expression
????[::] delete [ ] cast-expression
??????????delete operator??????????????????????????(???????????????????棬???????????????????????????檔??C++?У???new operator?????????棬???????delete operator???????????delete operator??????????????????????????
????a. ?????????????????????????
????b. ????operator delete function????????(deallocate the memory)
????3. ????new/delete??ù??????Щ????????
????(1)???????operator new/delete function ?? new/delete operator ?
?????????????????????????????????/???????棬???????new/delete operator?? ???????new/delete??????У????????????????operator new/delete function???????????????/???????
????(2) ??delete operator??????鯔????new operator??????棬??????????????????м??operator new??operator delete???????????????д?ó???????
????(3) new operator???????????std::bad_alloc?????????????????????????operator new function??delete operator?????????????????delete???????
????(4) ????????汻delete?????????????(Dereference)???????????????????????3??????
????(5) delete?????(NULL)??????????????κκ?????
????(6) ?????????operator new/delete????????????(static)??????????????????麯??(virtual function)???????public?? protected?? private???????????
????4. ????????????????????Щ?????
????????
#include <stdio .h>
#include <stdlib .h>
void * operator new(size_t unSize)
{
printf("operator new called
");
return malloc(unSize);
}
void * operator new(size_t unSize?? int nLine?? const char * pFunc)
{
printf("operator new called?? line: %d?? func: %s
"??
nLine?? pFunc);
return malloc(unSize);
}
void operator delete(void * pMem)
{
printf("delete1
");
free(pMem);
}
class A
{
public:
A(int a = 0) :
_a(a)
{
printf("constructor called
");
}
{
printf("~A()
");
}
static void operator delete(void * pMem?? size_t unSize)
{
printf("delete2: %u
"?? unSize);
free(pMem);
}
private:
int _a;
};
class B: public A
{
public:
~B()
{
printf("~B()
");
}
int _b;
int _bb;
};
int main()
{
A * pA = new A(10);
printf("#######
");
A * pB = new (__LINE__?? __func__) B();
printf("#######
");
A * szA = new A[10];
printf("#######
");
delete pA;
printf("#######
");
delete pB;
printf("#######
");
delete [] szA;
printf("#######
");
char * pC = NULL;
delete pC;
}
??????
???·???
??????????????????
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