可能重复:
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;
}