#include <iostream>
using namespace std;
class A{
public:
int s;
// ~A(){}
};
int main(){
A *c = new A[10];
delete c;
return 0;
}
Code above can run successfully, but when i code will get an error. Who can tell me why?
#include <iostream>
using namespace std;
class A{
public:
int s;
// ~A(){}
};
int main(){
A *c = new A[10];
delete c;
return 0;
}
Code above can run successfully, but when i code will get an error. Who can tell me why?
The behaviour of your code is undefined.
You must write delete[] c;
if c
is a pointer to memory allocated with new[]
.
(Interestingly, some compilers sort out this mess for your, but don't rely on that since then you're not writing portable C++).