new, delete ,malloc & free

后端 未结 2 1060
面向向阳花
面向向阳花 2020-12-05 15:41

This question was asked to me in an interview:

In C++,

  1. what if we allocate memory using malloc and use delete<
2条回答
  •  孤城傲影
    2020-12-05 16:07

    1) Undefined behaviour but will probably "work" though. Destructors will get called on the memory being freed that pobably doesn't want to be deconstructed.
    2) Undefined behaviour but will probably "work" though. Destructors will NOT get called.

    ie IF it works, and there is no guarantee of that, then it will only, likely, work exactly as required for basic builtin data types.

提交回复
热议问题