| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
跟大牛学C++学算法数据结构
共有 260 人关注过本帖
标题:调用operator new noexcept 的问题
只看楼主 加入收藏
花脸
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:7
帖 子:775
专家分:778
注 册:2017-1-4
结帖率:95.33%
  已结贴   问题点数: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;
}

2019-01-07 20:19
Jonny0201
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:36
帖 子:446
专家分:2241
注 册: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.cppreference.com/w/cpp/language/delete
2019-01-07 22:50
花脸
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:7
帖 子:775
专家分:778
注 册:2017-1-4
  得分:0 
回复 2楼 Jonny0201
看懂了,但不是很理解。
2019-01-07 23:07
Jonny0201
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:36
帖 子:446
专家分:2241
注 册:2016-11-7
  得分:0 
回复 3楼 花脸
就是 C++ 14 之前, 我估计你编译器应该不支持吧, 默认都是用 throw 的那一版本
2019-01-08 01:34
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:291
帖 子:6163
专家分:35550
注 册: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
等 级:蝙蝠侠
威 望:7
帖 子:775
专家分:778
注 册:2017-1-4
  得分:0 
回复 4楼 Jonny0201
好的。谢谢。
2019-01-08 10:44
花脸
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:7
帖 子:775
专家分:778
注 册:2017-1-4
  得分:0 
回复 5楼 rjsp
好的 谢谢。
2019-01-08 10:44







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

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