Difference between global non-throwing ::operator new and std::malloc

前端 未结 3 1580
一生所求
一生所求 2020-12-05 03:00

C++ has several functions to acquire dynamic storage, most of which differ in some fundamental way. Several more are usually added by the OS.

Two of these are of spe

3条回答
  •  盖世英雄少女心
    2020-12-05 03:16

    The main differences, aside from syntax and free vs. delete, are

    1. you can portably replace ::operator new;
    2. malloc comes with realloc, for which new has no equivalent;
    3. new has the concept of a new_handler, for which there is no malloc equivalent.

    (Replacing malloc opens up a can of worms. It can be done, but not portably, because it requires knowledge of the linker.)

提交回复
热议问题