所有 g++ 版本的 delete[] 可能存在错误或未定义此行为?
Possible bug on delete[] for all g++ versions or not defined behaviour for this?
我有这段代码,它给出了 3 个或更多元素的分段错误。我在 vs 和 clang 上测试并工作(循环结束和二进制结束没有错误)。我做错了什么?或者它是一个 g++ 错误?
如果我将 delete[]
行更改为 delete[] static_cast<B*>(a);
它也适用于 g++。但是,在实际情况下,我不知道真正的类型,所以我不能转换成任何东西。
class A {
public:
virtual ~A() {}
virtual int x() = 0;
};
class B : public A {
public:
B() : _x(1) {}
virtual ~B() {}
virtual int x() { return _x; }
private:
int _x;
};
int main(int argc, char * argv[]) {
A * a;
for (unsigned int i = 1; i <= 10; ++i) {
a = new B[i];
delete[] a;
}
return 0;
}
I 'm doing something wrong? or its a g++ bug?
你程序的行为是undefined:
If the static type of the object that is being deleted differs from
its dynamic type (such as when deleting a polymorphic object through a
pointer to base), and if the destructor in the static type is virtual,
the single object form of delete begins lookup of the deallocation
function's name starting from the point of definition of the final
overrider of its virtual destructor. Regardless of which deallocation
function would be executed at run time, the statically visible version
of operator delete must be accessible in order to compile. In other
cases, when deleting an array through a pointer to base, or when
deleting through pointer to base with non-virtual destructor, the
behavior is undefined.
来自 C++ 标准:
In a single-object delete expression, if the static type of the object to be deleted is different from its dynamic
type, the static type shall be a base class of the dynamic type of the object to be deleted and the static type
shall have a virtual destructor or the behavior is undefined. In an array delete expression, if the dynamic
type of the object to be deleted differs from its static type, the behavior is undefined.
(强调)这正是您在 delete[] a;
时所做的,分段错误绝对是未定义行为的可能结果。
我有这段代码,它给出了 3 个或更多元素的分段错误。我在 vs 和 clang 上测试并工作(循环结束和二进制结束没有错误)。我做错了什么?或者它是一个 g++ 错误?
如果我将 delete[]
行更改为 delete[] static_cast<B*>(a);
它也适用于 g++。但是,在实际情况下,我不知道真正的类型,所以我不能转换成任何东西。
class A {
public:
virtual ~A() {}
virtual int x() = 0;
};
class B : public A {
public:
B() : _x(1) {}
virtual ~B() {}
virtual int x() { return _x; }
private:
int _x;
};
int main(int argc, char * argv[]) {
A * a;
for (unsigned int i = 1; i <= 10; ++i) {
a = new B[i];
delete[] a;
}
return 0;
}
I 'm doing something wrong? or its a g++ bug?
你程序的行为是undefined:
If the static type of the object that is being deleted differs from its dynamic type (such as when deleting a polymorphic object through a pointer to base), and if the destructor in the static type is virtual, the single object form of delete begins lookup of the deallocation function's name starting from the point of definition of the final overrider of its virtual destructor. Regardless of which deallocation function would be executed at run time, the statically visible version of operator delete must be accessible in order to compile. In other cases, when deleting an array through a pointer to base, or when deleting through pointer to base with non-virtual destructor, the behavior is undefined.
来自 C++ 标准:
In a single-object delete expression, if the static type of the object to be deleted is different from its dynamic type, the static type shall be a base class of the dynamic type of the object to be deleted and the static type shall have a virtual destructor or the behavior is undefined. In an array delete expression, if the dynamic type of the object to be deleted differs from its static type, the behavior is undefined.
(强调)这正是您在 delete[] a;
时所做的,分段错误绝对是未定义行为的可能结果。