GCC4.8对new和delete的参数匹配新要求

一段通讯协议的代码,早年在GCC 4.4。VS2013下编译都挺好的,移植到GCC 4.8 ,为C++ 11作准备,在编译的时候发现问题git

源代码省略后的版本以下:app

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();

ui

google了一下错误信息发现了一个比较清晰的解释。google

http://stackoverflow.com/questions/5367674/what-does-the-error-non-placement-deallocation-functionspa

能够翻译为GCC支持0x标准后,任务placement new有2个参数,对应的delete应该是2个参数,(第二个为size),非placement的new,对应的delete不能是2个参数。翻译

而个人代码里面的new是new一个变长的数据,(这是一个协议头)因此不是placement的new,因此也就能有std::size_t参数。把代码改成:code

    //重载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上编译,结果发现了以下告警:blog

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:\courage\zcelib\zcelib.git\src\commlib\soarlib\soar_zerg_frame.h(339) : see declaration of 'Zerg_App_Frame::operator new'字符串

 

看来VC++尚未跟上脚步。

又只有用ifdef写晦涩的兼容代码。TNNNNNNND。

====================================

原来写了ifdef 熬过去了。

2018年更新一下VS,升级到了VS2017,发现一个更加好玩的事情。编译

new errors : C2956 : sized deallocation function。

static void   *operator new (size_t , size_t lenframe = LEN_OF_APPFRAME_HEAD);

static void operator delete(void *ptrframe,size_t);

问题的缘由 在这个地方 https://msdn.microsoft.com/en-us/library/mt723604.aspx 有说明。
发现VC++为了适应C++14,又不容许你的 delete 出现
delete(void *ptrframe,size_t); 这种方式了。即便在类里面。但你改为static void operator delete(void *ptrframe);却仍然有 C4291的告警。看了一下推荐的修改方式,其实也是扰的方式。心中默默的竖起了一个中指。
相关文章
相关标签/搜索