当我们有new/delete时,为什么要使用malloc/free? [英] Why use malloc/free, when we have new/delete?
本文介绍了当我们有new/delete时,为什么要使用malloc/free?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我们在C ++中具有new
和delete
时,malloc
和free
的用途是什么.我想free
和delete
的功能都相同.
What is the use of malloc
and free
when we have new
and delete
in C++. I guess function of both free
and delete
is same.
推荐答案
它们不一样. new
调用构造函数,malloc
只是分配内存.
They're not the same. new
calls the constructor, malloc
just allocates the memory.
此外,未定义的行为将两者混在一起(即,将new
与free
一起使用,并且将malloc
与delete
一起使用).
Also, it's undefined behavior mixing the two (i.e. using new
with free
and malloc
with delete
).
在C ++中,出于与C的兼容性原因,应该使用new
和delete
,malloc
和free
.
In C++, you're supposed to use new
and delete
, malloc
and free
are there for compatibility reasons with C.
这篇关于当我们有new/delete时,为什么要使用malloc/free?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文