#include <stdio.h>
#include <iostream>
static unsigned int totalCount = 0;
struct TestStruct
{
public:
TestStruct()
{
}
~TestStruct()
{
}
};
void* operator new(size_t size)
{
printf("(Enter new)\n");
++totalCount;
char* alloced = nullptr;
try
{
alloced = (char*)malloc(size);
}
catch(std::bad_alloc exception)
{
throw(exception);
}
return alloced;
}
void operator delete(void* pointer)
{
printf("(Enter delete)\n");
--totalCount;
try
{
free(pointer);
}
catch(std::exception s)
{
throw(s);
}
}
int main(int argc, const char** argv)
{
printf("This is a sample for rewirte operator new and delete\n");
TestStruct* sss = new TestStruct();
delete sss;
if (totalCount != 0)
{
printf("Trigger Assertion!\n");
assert(totalCount == 0);
}
printf("Exit Normaly!\n");
return 0;
}
C++内存泄露检测
最新推荐文章于 2024-08-21 22:18:43 发布