<分区>
Possible Duplicate:
What is the difference between new/delete and malloc/free?
它们在功能上是否相同,唯一的区别是调用 delete 时需要使用 [] 运算符,还是我还遗漏了什么?
谢谢
<分区>
Possible Duplicate:
What is the difference between new/delete and malloc/free?
它们在功能上是否相同,唯一的区别是调用 delete 时需要使用 [] 运算符,还是我还遗漏了什么?
谢谢
最佳答案
正如 Mehrdad 在 this question: 中所说的那样
malloc allocates uninitialized memory. The allocated memory has to be released with free.
new initializes the allocated memory by calling the constructor (if it's an object). Memory allocated with new should be released with delete (which in turn calls the destructor). It does not need you to manually specify the size you need and cast it to the appropriate type. Thus, it's more modern and less prone to errors.
NOTE:- new is an operator, malloc is a function
关于c++ - "new int[5]"和 "malloc(5 * sizeof(int))"之间的 C++ 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12982003/