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

一段通信协议的代码,早年在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:\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的告警。
看了一下推荐的修改方式,其实也是扰的方式。

心中默默的竖起了一个中指。

 

posted @ 2015-02-14 21:52  fullsail  阅读(1721)  评论(2编辑  收藏  举报