更新时间:2023-11-13 14:02:40
否,在堆栈分配的变量上调用 delete
是不安全的.你应该只对 new
创建的东西调用 delete
.
No, it is not safe to call delete
on a stack-allocated variable. You should only call delete
on things created by new
.
malloc
或 calloc
,应该只有一个 free
.new
,应该恰好有一个 delete
.new[]
,应该恰好有一个 delete[]
.malloc
or calloc
, there should be exactly one free
. new
there should be exactly one delete
. new[]
there should be exactly one delete[]
. 一般情况下,您不能混合搭配其中任何一种,例如没有 free
-ing 或 delete[]
-ing new
对象.这样做会导致未定义的行为.
In general, you cannot mix and match any of these, e.g. no free
-ing or delete[]
-ing a new
object. Doing so results in undefined behavior.