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写晦涩的兼容代码。

Linux升级GCC 4.8.1清晰简明教程(Ubuntu 12.04 64位版为例) 

在CentOS 6.4中编译安装GCC 4.8.1 + GDB 7.6.1 + Eclipse 在CentOS 6.4中编译安装GCC 4.8.1 + GDB 7.6.1 + Eclipse

Ubuntu下Vim+GCC+GDB安装及使用 

Ubuntu下两个GCC版本切换 

CentOS6.5升级手动安装GCC4.8.2 

GCC 的详细介绍:请点这里
GCC 的下载地址:请点这里

本文永久更新链接地址:

相关内容