当我们有new/delete时,为什么要使用malloc/free? [英] Why use malloc/free, when we have new/delete?

查看:94
本文介绍了当我们有new/delete时,为什么要使用malloc/free?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们在C ++中具有newdelete时,mallocfree的用途是什么.我想freedelete的功能都相同.

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.

此外,未定义的行为将两者混在一起(即,将newfree一起使用,并且将mallocdelete一起使用).

Also, it's undefined behavior mixing the two (i.e. using new with free and malloc with delete).

在C ++中,出于与C的兼容性原因,应该使用newdeletemallocfree.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆