没有 delete() 的 new() 是未定义行为还是仅仅是内存泄漏?

new() without delete() is Undefined Behavior or merely Memory Leak?(没有 delete() 的 new() 是未定义行为还是仅仅是内存泄漏?)

本文介绍了没有 delete() 的 new() 是未定义行为还是仅仅是内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
内存泄漏是未定义行为"吗?C++中的类问题?

永远不要对 newnew [] 返回的地址调用 deletedelete[]C++ 程序是未定义行为还是仅仅是内存泄漏?

Never calling delete or delete[] on address returned by new or new [] resp in a C++ program is an Undefined Behavior or merely a memory leak?

欢迎参考标准(如果有).
这出现在这里的评论之一中.我只是对此有点困惑.

References from the Standard(if any) are welcome.
This came up in one of the comments here & I am just a bit confused about it.

推荐答案

[basic.life](3.8 对象生命周期)第4段讲:

[basic.life] (3.8 Object lifetime) in paragraph 4 tells :

程序可以通过重用对象占用的存储或通过显式调用具有非平凡析构函数的类类型对象的析构函数来结束任何对象的生命周期.对于具有非平凡析构函数的类类型的对象,在该对象占用的存储空间被重用或释放之前,程序不需要显式调用析构函数;但是,如果没有显式调用析构函数或者如果没有使用删除表达式(5.3.5)来释放存储,则不应隐式调用析构函数,并且任何依赖于副作用的程序都不会产生由析构函数未定义行为.

A program may end the lifetime of any object by reusing the storage which the object occupies or by explicitly calling the destructor for an object of a class type with a non-trivial destructor. For an object of a class type with a non-trivial destructor, the program is not required to call the destructor explicitly before the storage which the object occupies is reused or released; however, if there is no explicit call to the destructor or if a delete-expression (5.3.5) is not used to release the storage, the destructor shall not be implicitly called and any program that depends on the side effects produced by the destructor has undefined behavior.

这篇关于没有 delete() 的 new() 是未定义行为还是仅仅是内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:没有 delete() 的 new() 是未定义行为还是仅仅是内存泄漏?

基础教程推荐