-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
  • 6,115
  • 16
  • 50
  • 57

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
  • 231,907
  • 34
  • 361
  • 483