出現的問題bad_alloc();
在這裏G++裏面沒有bad_alloc(const char * _Message)構造函數
#include<iostream>
//#include<memory>
#include<cstdlib>
using namespace std;
void *operator new(size_t size)
{
void *p = NULL;
cout << " 是我申請了內存哦 " << endl;
if (NULL == (p = malloc(size)))
{
throw std::bad_alloc();
/*//std::bad_alloc(" ") g++ 中沒有bad_alloc(const char *masg);構造函數, 在vs中可以查找到聲明,但是是私有的所以外界不能調用*/
}
return p;
}
void operator delete(void *ptr)
{
free(ptr);
cout << "我釋放了p,你怕不怕";
}
int main(int argc, char **argv)
{
int *p1 = new int;//作用域的問題只要有重載,默認優先調用重載函數,若沒有則調用全局
cout << sizeof(p1) << endl;
delete(p1);
//int * p2 = new int[100000*11111111111000004000];
//在這裏不會讓你觸發這麼大的內存的哈哈哈
system("pause");
return 0;
}
好了記錄就到這裏