| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2020 人关注过本帖
标题:调用operator new noexcept 的问题
只看楼主 加入收藏
花脸
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:9
帖 子:788
专家分:907
注 册:2017-1-4
结帖率:95.37%
收藏
已结贴  问题点数:20 回复次数:6 
调用operator new noexcept 的问题
程序代码:
#include <iostream>
#include <new>
#include <cstdio>
using namespace std;

struct A
{
    void *operator new(size_t size) 
    {
        cout<<"operator new"<<endl;
        malloc(size);
    }
    void operator delete(void *p)
    {
        cout<<"operator delete"<<endl;
        free(p);
    }
    
    void *operator new(size_t size,const nothrow_t &thorw_value) noexcept
    {
        cout<<"operator new noexcept"<<endl;
        malloc(size);
    }
    void operator delete(void *p,const nothrow_t &nowthrow_value) throw()
    {
        cout<<"operator delete noexpect."<<endl;
        free(p);
    }
}; 

int main()
{
    A *a1=new A;
    delete a1;
    
    A *a2=new(nothrow) A;
    delete a2;//为什么调用的是 operator delete 而不是  operator delete noexpect
    return 0;
}

搜索更多相关主题的帖子: operator new void size delete 
2019-01-07 20:19
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
收藏
得分:10 
你这个代码里可全是 UB...
After that, whether or not an exception was thrown by any destructor, the delete expression invokes the deallocation function: either operator delete (for the first version of the expression) or operator delete[] (for the second version of the expression), unless the matching new-expression was combined with another new-expression (since C++14)

https://en.
2019-01-07 22:50
花脸
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:9
帖 子:788
专家分:907
注 册:2017-1-4
收藏
得分:0 
回复 2楼 Jonny0201
看懂了,但不是很理解。
2019-01-07 23:07
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
收藏
得分:0 
回复 3楼 花脸
就是 C++ 14 之前, 我估计你编译器应该不支持吧, 默认都是用 throw 的那一版本
2019-01-08 01:34
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:10 
两处 malloc(size) 应该是 return malloc(size),你的代码编译过吗?

特殊的 operator delete 是在对应的 operator new 成功,但 构造函数 失败时才被调用。你想一想吧,编译怎么可能知道一个指针内存是哪个重载的operator new分配的?
程序代码:
#include <iostream>
#include <new>
using namespace std;

struct A
{
    A( bool xpt )
    {
        if( xpt )
            throw( xpt );
    }

    void *operator new(size_t size)
    {
        cout<<"operator new"<<endl;
        return malloc(size);
    }
    void operator delete(void *p)
    {
        cout<<"operator delete"<<endl;
        free(p);
    }
  

    void *operator new(size_t size,const nothrow_t &thorw_value) noexcept
    {
        cout<<"operator new noexcept"<<endl;
        return malloc(size);
    }
    void operator delete(void *p,const nothrow_t &nowthrow_value) throw()
    {
        cout<<"operator delete noexpect."<<endl;
        free(p);
    }
};

int main()
{
    A* a1 = new A(false);
    delete a1;

    try {
        A* a2 = new A(true);
        delete a2;
    }
    catch( ... )
    {
    }
  

    A* a3 = new(nothrow) A(false);
    delete a3;

    try {
        A* a4 = new(nothrow) A(true);
        delete a4;
    }
    catch( ... )
    {
    }

    return 0;
}
输出
operator new
operator delete
operator new
operator delete
operator new noexcept
operator delete
operator new noexcept
operator delete noexpect.
2019-01-08 09:37
花脸
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:9
帖 子:788
专家分:907
注 册:2017-1-4
收藏
得分:0 
回复 4楼 Jonny0201
好的。谢谢。
2019-01-08 10:44
花脸
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:9
帖 子:788
专家分:907
注 册:2017-1-4
收藏
得分:0 
回复 5楼 rjsp
好的 谢谢。
2019-01-08 10:44
快速回复:调用operator new noexcept 的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027262 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved