使用new需要您也调用delete吗? [英] What does the use of new require you to also call delete?

查看:137
本文介绍了使用new需要您也调用delete吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++书中遇到以下问题:

I am here stuck with a question in my C++ book with the following:

使用新功能需要您同时调用删除吗?"

"What does the use of new require you to also call delete?"

也许您对此有答案?

推荐答案

因为这是C ++的设计方式&这是预期的行为.
目的是提供您需要并拥有的内存分配,直到您明确地分配它为止.

Because that is the way C++ is designed & that is the intended behavior.
The intention was to provide a memory allocation which you demand and own till you reliquish it explicitly.

new为您提供(在堆上)动态内存分配,该内存分配将继续存在并拥有它,直到您通过调用delete显式取消分配它为止. 未能在new缓冲区上调用delete会导致导致 Undefined Behaviors (不确定行为),通常以的形式出现. 1 内存泄漏.

new gives you a dynamic memory allocation(on heap) which will continue to exist and you own it untill you explicitly deallocate it by calling delete.
Failing to call a delete on a newed buffer will lead to Undefined Behaviors usually in the form of. 1 memory leaks.

1 此处中对此进行了讨论.

1 This was discussed here.

这篇关于使用new需要您也调用delete吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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