加入收藏 | 设为首页 | 会员中心 | 我要投稿 南通站长网 (https://www.0513zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

GCC4.8对new和delete的参数匹配新条件

发布时间:2021-11-19 16:11:41 所属栏目:教程 来源:互联网
导读:一段通信协议的代码,早年在GCC 4.4。VS2013下编译都挺好的,移植到GCC 4.8 ,为C++ 11做准备,在编译的时候发现问题 源代码省略后的版本如下: class Zerg_App_Frame { public: //重载New函数 static void *operator new (size_t , size_t lenframe = LEN_OF

一段通信协议的代码,早年在GCC 4.4。VS2013下编译都挺好的,移植到GCC 4.8 ,为C++ 11做准备,在编译的时候发现问题
 
源代码省略后的版本如下:
 
class Zerg_App_Frame
{
public:
 
 
    //重载New函数
    static void  *operator new (size_t , size_t lenframe = LEN_OF_APPFRAME_HEAD);
    //不重载delte与情理不通,但是其实没有什么问题,
    static void operator delete(void *ptrframe,size_t);
   
public:
 
    //整个通讯包长度,留足空间,包括帧头的长度.
    uint32_t              frame_length_;
 
    //frame_appdata_ 是一个变长度的字符串序列标示,
#ifdef ZCE_OS_WINDOWS
#pragma warning ( disable : 4200)
#endif
    char                frame_appdata_[];
#ifdef ZCE_OS_WINDOWS
#pragma warning ( default : 4200)
#endif
 
};
 
 
 
GCC(G++) 4.8编译提示的错误如下,
 
soar_zerg_frame.h:260:17: error: non-placement deallocation function ‘static void Zerg_App_Frame::operator delete(void*, size_t)’ [-fpermissive]
    static void operator delete(void *ptrframe,size_t);
                ^
In file included from soar_svrd_app_fsm_notify.cpp:3:0:
soar_zerg_frame_malloc.h:323:86: error: selected for placement delete [-fpermissive]
        Zerg_App_Frame *proc_frame = new(size_appframe_[list_no] + 1) Zerg_App_Frame();
 
google了一下错误信息发现了一个比较清晰的解释。
 
http://stackoverflow.com/questions/5367674/what-does-the-error-non-placement-deallocation-function
 
可以翻译为GCC支持0x标准后,任务placement new有2个参数,对应的delete应该是2个参数,(第二个为size),非placement的new,对应的delete不能是2个参数。
 
而我的代码里面的new是new一个变长的数据,(这是一个协议头)所以不是placement的new,所以也就能有std::size_t参数。把代码改为:
 
    //重载New函数
    static void  *operator new (size_t , size_t lenframe = LEN_OF_APPFRAME_HEAD);
    //不重载delte与情理不通,但是其实没有什么问题,
    static void operator delete(void *ptrframe);
 
G++编译通过,OK,但回到VC++ 2013上编译,结果发现了如下告警:
 
2>soar_zerg_frame.cpp(395): warning C4291: 'void *Zerg_App_Frame::operator new(size_t,size_t)' : no matching operator delete found; memory will not be freed if initialization throws an exception
2>          e:couragezcelibzcelib.gitsrccommlibsoarlibsoar_zerg_frame.h(339) : see declaration of 'Zerg_App_Frame::operator new'
 
看来VC++还没有跟上脚步。
 
又只有用ifdef写晦涩的兼容代码。

(编辑:南通站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读