c++ - "new int[5]"和 "malloc(5 * sizeof(int))"之间的 C++ 有什么区别?

标签 c++ c memory

<分区>

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/

相关文章:

linux - 在 Varnish 中获取每个域的内存使用情况

c++ - 为什么我的函数开始返回 nan?

c++ - 使用析构函数时双重释放或损坏

c - 为什么 CodeBlocks 会突出显示关键字 class 并在没有任何错误的情况下编译它(我正在用 C 编程)?

c - 为什么 strncpy not null 终止?

c - 程序运行功能

memory - CUDA 共享内存和全局内存有什么区别?

multithreading - NSOperationQueue调用performSelectorOnMainThread导致应用程序崩溃

c++ - 我如何摆脱此错误main.cpp :43:19: error: no viable overloaded '=' novowels[100] = remove(name[100]);

c++ - 模板类中没有匹配的函数