-2
#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?

Kara
  • 5,996
  • 16
  • 49
  • 56

1 Answers1

2

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++).

Bathsheba
  • 227,678
  • 33
  • 352
  • 470