Usando delete
Há duas variantes sintáticas para o operador Delete: uma para objetos únicos e o outra para matrizes de objetos. O fragmento de código a seguir mostra como elas são diferentes:
// expre_Using_delete.cpp
struct UDType
{
};
int main()
{
// Allocate a user-defined object, UDObject, and an object
// of type double on the free store using the
// new operator.
UDType *UDObject = new UDType;
double *dObject = new double;
// Delete the two objects.
delete UDObject;
delete dObject;
// Allocate an array of user-defined objects on the
// free store using the new operator.
UDType (*UDArr)[7] = new UDType[5][7];
// Use the array syntax to delete the array of objects.
delete [] UDArr;
}
Os dois casos a seguir produzem resultados indefinidos: usando o formato da matriz de exclusão (delete [ ]) em um objeto e usando o formulário que não de matriz de exclusão em uma matriz.