【发布时间】:2012-10-10 12:37:03
【问题描述】:
可能重复:
What is the difference between new/delete and malloc/free?
它们在功能上是否相同,是调用 delete 时需要使用 [] 运算符的唯一区别,还是我还缺少其他东西?
谢谢
【问题讨论】:
-
切勿将
delete或delete []与malloc一起使用!使用free。 -
malloc是 C 主义。它需要强制转换并且应该与free匹配,而不是delete/delete[]。 (你有什么理由不使用std::vector<int>?) -
new确保调用对象的构造函数! -
This pretty much answers this,以及您可能关于的所有其他问题。
-
这可能会让你大吃一惊,但
new int[5];和new int[5]();也有非常相关的区别。