使用new需要您也调用delete吗? [英] What does the use of new require you to also call 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 new
ed buffer will lead to Undefined Behaviors usually in the form of. 1 memory leaks.
1 This was discussed here.
这篇关于使用new需要您也调用delete吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!