目錄
malloc/free的用法:
//malloc/free的用法
#pragma region
{
void* p_malloc = malloc(512);
free(p_malloc);
p_malloc = nullptr;
}
#pragma endregion
new/delete的用法
//new/delete的用法
#pragma region
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
Show_SMALL_FUNCTION_BLOCK_TIPS("new/delete的用法")
#endif
void* p_new = new char;
delete p_new;
p_new = nullptr;
/*
關於delete什麼時候加[]
[]代表着多次調用析構函數
首先得是對象,其次有意義的。
*/
}
#pragma endregion
重載了類內的new/delete
//重載了類內局部的new和delete
#pragma region
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
Show_SMALL_FUNCTION_BLOCK_TIPS("重載了類內局部的new和delete")
#endif
class Complex
//類的內容區域
#pragma region
{
public:
int m_nData;
Complex():m_nData(0)
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
std::cout << "complex::Construct" << endl;
#endif
}
static void testnewTwoArgus()
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
std::cout << " testnewTwoArgus() " << "Current Line is:" << __LINE__ << endl;
std::cout << " testnewTwoArgus() throw int ";
#endif
throw 100;
}
Complex(int In_Data)
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
std::cout << "complex::Construct" << endl;
#endif
m_nData = In_Data;
}
void* operator new(std::size_t temp)
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
std::cout << "operator new " << "Current Line is:" << __LINE__ << endl;
#endif
return nullptr;
}
void* operator new(std::size_t temp,string strtemp)
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
std::cout << "override operator new two argues temp string " << "Current Line is:" << __LINE__ << endl;
std::cout << strtemp << endl;
#endif
return nullptr;
}
void operator delete(void* p, string strtemp)
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
std::cout << "override operator delete two argues temp string " << "Current Line is:" << __LINE__ << endl;
std::cout << strtemp << endl;
#endif
testnewTwoArgus();
}
void operator delete(void* p, std::size_t temp)
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
std::cout << "override operator delete " << "Current Line is:" << __LINE__ << endl;
#endif
}
void* operator new[](std::size_t temp)
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
std::cout << "override operator new[] " << "Current Line is:" << __LINE__ << endl;
#endif
return nullptr;
}
void operator delete[](void* p, std::size_t temp)
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
std::cout << "override operator delete[] " << "Current Line is:" << __LINE__ << endl;
#endif
}
private:
};
#pragma endregion
//測試重寫operator new
#pragma region
{
Complex* pc = new Complex[10];
delete[]pc;
}
{
Complex* pc = new Complex;
delete pc;
}
{
Complex* pc = ::new Complex;
::delete pc;
}
{
Complex* pc = ::new Complex[10];
::delete[]pc;
}
#pragma endregion
}
#pragma endregion
重載了PlacementNew
//重載PlacementNew
#pragma region
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
Show_SMALL_FUNCTION_BLOCK_TIPS("重載PlacementNew實例")
#endif
class Complex
//類的內容區域
#pragma region
{
public:
int m_nData;
Complex() :m_nData(0)
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
std::cout << "complex::Construct" << endl;
#endif
throw (int)100;
}
Complex(int In_Data)
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
std::cout << "complex::Construct" << endl;
#endif
m_nData = In_Data;
}
void* operator new(std::size_t temp, string strtemp)
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
std::cout << "override operator new two argues temp string " << "Current Line is:" << __LINE__ << endl;
std::cout << strtemp << endl;
#endif
return malloc(temp);
}
void operator delete(void* p, string strtemp)
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
std::cout << "override operator delete two argues temp string " << "Current Line is:" << __LINE__ << endl;
std::cout << strtemp << endl;
#endif
}
private:
};
#pragma endregion
//注意,此處多態重載了operator delete以後不能手動用的,只有在你構造函數throw了異常,會自動調用
#pragma region
{
try {
Complex* pc = new("hello") Complex();
}
catch (int a) {
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
std::cout << "test operator delete two argus catch " << a << endl;
#endif
a;
}
}
#pragma endregion
}
#pragma endregion
全局::operator new和delete的用法
#pragma region
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
Show_SMALL_FUNCTION_BLOCK_TIPS("全局::operator new和delete的用法")
#endif
void *p_operatornew = ::operator new(512);
::operator delete(p_operatornew);
p_operatornew = nullptr;
}
#pragma endregion
Alloctor用法
//alloctor基本用法
#pragma region
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
Show_SMALL_FUNCTION_BLOCK_TIPS("alloctor的用法")
#endif
int* p_int = allocator<int>().allocate(1);
allocator<int>().deallocate(p_int, 1);
}
#pragma endregion
PlacementNew基本用法
//placementnew
//我們可以將對象構建在已經分配了內存的內存區。
#pragma region
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
Show_SMALL_FUNCTION_BLOCK_TIPS("placementnew的基本用法")
#endif
class Complex
{
public:
int m_nData;
Complex() :m_nData(0)
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
cout << "complex::Construct" << endl;
#endif
}
Complex(int In_Data)
{
#ifdef SHOW_FUNCTION_BaseUsage_DEBUG_TIPS
cout << "complex::Construct" << endl;
#endif
m_nData = In_Data;
}
private:
};
char* buff = new char[sizeof(Complex) * 3];
Complex* pc = new(buff) Complex();
/*等同於
void* p_complex = ::operator new(sizeof(Complex),buff);//2個參數
Complex* pc = static_cast<Complex*>(p_complex);
pc->Complex::Complex();
*/
delete []buff;
}
new_handler的使用
//小例子:new_handler的使用,newhandler是函數指針的用法 用於new失敗以後的自定義處理,因爲在老式編譯器裏面會返回0,不會拋出異常。
#pragma region
{
//typedef void* (*new_handler)();
//new_handler set_new_handler(new_handler p)throw();
set_new_handler(PerClassAlloctorData::NoMemory);
//調用下面的函數直接跑滿內存並且觸發了你的函數
/*
for (size_t i = 0; i < 1000000000; i++)
{
int* p = new int[1000000000];
}*/
}
#pragma endregion