zoukankan      html  css  js  c++  java
  • 对于重载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;
    }

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

  • 相关阅读:
    checkedListBox 的用发
    C# 控件命名规范
    控件数据及相应的事件处理
    MDI 窗口的创建
    摄像头中运动物体识别
    1
    静态检测大风车初版
    不会难道我还不能附上链接吗
    计算机操作素材
    数字识别
  • 原文地址:https://www.cnblogs.com/VCctor/p/5100690.html
Copyright © 2011-2022 走看看