删除ptr vs delete [] ptr

时间:2012-07-27 06:20:36

标签: c++ new-operator delete-operator

  

可能重复:
  Is delete[] equal to delete?

int main()
{
    char *ptr = new char[10];

    delete ptr;  // or delete [] ptr;
}

delete []用于数组,delete用于单个对象,对吧?因此,在上面的代码中它应该是delete [] ptr;,但似乎delete ptr;也可以。不可思议?

此外,

int main()
{
    int x;
    cin >> x;
    char *ptr = new char[x];  //cannot make sure whether it is a char pointer or a pointer to an array, right?
    // delete ptr, or delete [] ptr;
}

0 个答案:

没有答案