I have this code who gives me a segmentation fault 3 or more elements. I tested on vs and clang and works (loop ends and binary ends with no errors). I 'm doing something wrong? or its a g++ bug?
If i change the delete[]
line to delete[] static_cast<B*>(a);
it works on g++ too. But, in real cases, I will do not know the real type so I can not cast to anything.
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;
}