“新”和“新”之间的区别是什么?和“malloc”和“calloc”在C ++? [英] What is the difference between "new" and "malloc" and "calloc" in C++?
本文介绍了“新”和“新”之间的区别是什么?和“malloc”和“calloc”在C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
new和malloc和calloc和其他人在家庭中有什么区别?
What is the difference between "new" and "malloc" and "calloc" and others in family?
(何时)
推荐答案
> new
和 delete
是C ++特有的功能。他们不存在于C. malloc
是老学校C做事的方式。大多数情况下,您不需要在C ++中使用它。
new
and delete
are C++ specific features. They didn't exist in C. malloc
is the old school C way to do things. Most of the time, you won't need to use it in C++.
-
malloc
分配未初始化的内存。必须使用free
发布分配的内存。 -
calloc
malloc
,但用常量(0)初始化分配的内存。需要释放free
。 -
new
初始化分配的内存通过调用构造函数(如果它是一个对象)。分配有new
的内存应该使用delete
(这反过来又调用析构函数)释放。它不需要您手动指定所需的大小,并将其转换为适当的类型。因此,它更现代,更不容易出错。
malloc
allocates uninitialized memory. The allocated memory has to be released withfree
.calloc
is likemalloc
but initializes the allocated memory with a constant (0). It needs to be freed withfree
.new
initializes the allocated memory by calling the constructor (if it's an object). Memory allocated withnew
should be released withdelete
(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.
这篇关于“新”和“新”之间的区别是什么?和“malloc”和“calloc”在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文