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;
}