What is the “correct” way to reconcile malloc and new in a mixed C/C++ program?

后端 未结 3 573
生来不讨喜
生来不讨喜 2020-12-16 13:18

I have a mixed C/C++ program. It contains a flex/bison parser which targets C, while the remainder is C++.

Being C, the generated parser and scanner manage their me

3条回答
  •  悲&欢浪女
    2020-12-16 14:02

    Ok. Dug up an old working draft of the standard (2/28/2011 rev 3242.) It appears the relevant sections are 3.7.4 Dynamic storage duration and 18.6.1 Storage allocation and deallocation.

    In short it seems the pan-galactic hook I wanted are the global new and delete operators themselves. If one respects some semantics (in 3.7.4.1 and 3.7.4.2: basically delegate to new_handler as needed) one is allowed to replace

    void* operator new(std::size_t);
    void* operator new[](std::size_t);
    void operator delete(void*);
    void operator delete[](void*);
    

    to arrest default memory management of the entire C++ program. I still can't find the section that proves @paxdiablo right, but I'm willing to run with it for now.

提交回复
热议问题