new() without delete() is Undefined Behavior or merely Memory Leak?(没有 delete() 的 new() 是未定义行为还是仅仅是内存泄漏?)
问题描述
可能的重复:
内存泄漏是未定义行为"吗?C++中的类问题?
永远不要对 new
或 new []
返回的地址调用 delete
或 delete[]
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() 是未定义行为还是仅仅是内存泄漏?


基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01