☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

对象生命周期已结束,但未调用其有副作用的析构函数

C++-Undefined-Behavior-18
目录 › next › previous

示例:

struct U {
    ....
};

struct T {
   ~T();     // If it has side effects
    ....
};

void* p = malloc(max(sizeof(T), sizeof(U)));
T* pT = new (p) T;
U* pU = new (p) U;   // Undefined behavior

例中第二个 new 表达式结束了 pT 所指对象的生命周期,但没有调用其析构函数,如果其析构函数存在副作用,则会导致未定义的行为。

依据

ISO/IEC 14882:2003 3.8(4)-undefined ISO/IEC 14882:2011 3.8(4)-undefined
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.