对于重载new函数所遇到的问题记录

出现的问题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;
}

这里写图片描述
好了记录就到这里

posted @ 2015-11-08 14:39  齐庆  阅读(190)  评论(0编辑  收藏  举报