class Base
{
public:
int x;
int y;
virtual ~Base()
{
}
};
class Derived : public Base
{
public:
int z;
};
int main()
{
Base* ptr = new Derived[10];
delete[] ptr;
}
像这样会不会有内存泄露,今天看 inside c++ object model 说这样会泄露,难道语言上没有对这种情况做处理?
...全文
14319打赏收藏
问一个关于delete的问题
class Base { public: int x; int y; virtual ~Base() { } }; class Derived : public Base { public: int z; }; int main() { Base* ptr = new Derived[10]; delete[] ptr; } 像这样会不会有内存泄露,今天看 inside c++ object model 说这样会泄露,难道语言上没有对这种情况做处理?
VS 能通过并不能说明什么,cygwin 下就挂了。
[/Quote]
ISO/IEC 14882:2003(E) 5.3.5 Delete
In the first alternative (delete object), if the static type of the operand is different from its dynamic type, the static type shall be a base class of the operand’s dynamic type and the static type shall have a virtual
destructor or the behavior is undefined. In the second alternative (delete array) if the dynamic type of the object to be deleted differs from its static type, the behavior is undefined.73)