delete — Operator (C++)delete Operator (C++)
Zwalnia blok pamięci.Deallocates a block of memory.
SkładniaSyntax
[
::
]delete
wyrażenie cast[::
]delete
cast-expression
[::
]delete []
wyrażenie cast[::
]delete []
cast-expression
UwagiRemarks
Argument cast-expression musi być wskaźnikiem do bloku pamięci, który został wcześniej przydzielony dla obiektu utworzonego za pomocą operatora new.The cast-expression argument must be a pointer to a block of memory previously allocated for an object created with the new operator. delete
Operator ma wynik typu void
i w związku z tym nie zwraca wartości.The delete
operator has a result of type void
and therefore does not return a value. Na przykład:For example:
CDialog* MyDialog = new CDialog;
// use MyDialog
delete MyDialog;
Użycie delete
na wskaźniku do obiektu, którego nie przydzielono, new
daje nieprzewidywalne wyniki.Using delete
on a pointer to an object not allocated with new
gives unpredictable results. Można jednak użyć delete
na wskaźniku o wartości 0.You can, however, use delete
on a pointer with the value 0. Ten sposób oznacza, że gdy new
zwraca wartość 0 w przypadku niepowodzenia, usunięcie wyniku operacji zakończonej niepowodzeniem new
jest szkodliwe.This provision means that, when new
returns 0 on failure, deleting the result of a failed new
operation is harmless. Aby uzyskać więcej informacji, zobacz Operatory New i DELETE.For more information, see The new and delete Operators.
new
Operatory i delete
mogą być również używane dla typów wbudowanych, w tym tablic.The new
and delete
operators can also be used for built-in types, including arrays. Jeśli pointer
odwołuje się do tablicy, umieść puste nawiasy ( []
) przed pointer
:If pointer
refers to an array, place empty brackets ([]
) before pointer
:
int* set = new int[100];
//use set[]
delete [] set;
Użycie delete
operatora na obiekcie powoduje cofnięcie przydziału pamięci.Using the delete
operator on an object deallocates its memory. Program, który odwołuje się do wskaźnika po usunięciu obiektu, może mieć nieprzewidywalne wyniki lub awarię.A program that dereferences a pointer after the object is deleted can have unpredictable results or crash.
Gdy delete
jest używany do cofnięcia przydziału pamięci dla obiektu klasy języka C++, destruktor obiektu jest wywoływana przed cofnięciem alokacji pamięci obiektu (Jeśli obiekt ma destruktor).When delete
is used to deallocate memory for a C++ class object, the object's destructor is called before the object's memory is deallocated (if the object has a destructor).
Jeśli argument operacji delete
operatora jest modyfikowalną wartością l, jej wartość jest niezdefiniowana po usunięciu obiektu.If the operand to the delete
operator is a modifiable l-value, its value is undefined after the object is deleted.
Jeśli określono opcję kompilatora /SDL (Włącz dodatkowe kontrole zabezpieczeń) , operand do delete
operatora jest ustawiony na nieprawidłową wartość po usunięciu obiektu.If the /sdl (Enable additional security checks) compiler option is specified, the operand to the delete
operator is set to an invalid value after the object is deleted.
Używanie opcji usuwaniaUsing delete
Istnieją dwie warianty składni dla operatora delete: jeden dla pojedynczych obiektów i drugi dla tablic obiektów.There are two syntactic variants for the delete operator: one for single objects and the other for arrays of objects. Poniższy fragment kodu przedstawia różnice między nimi:The following code fragment shows how they differ:
// 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;
}
Dwa następujące przypadki generują niezdefiniowane wyniki: przy użyciu formy tablicowej Delete ( delete []
) dla obiektu oraz przy użyciu formy usuwania z tablicy.The following two cases produce undefined results: using the array form of delete (delete []
) on an object, and using the nonarray form of delete on an array.
PrzykładExample
Aby zapoznać się z przykładami użycia delete
, zobacz New Operator.For examples of using delete
, see new operator.
Jak działa usuwanieHow delete works
Operator delete wywołuje operator funkcji Delete.The delete operator invokes the function operator delete.
Dla obiektów, które nie są typu klasy (Klasa, Strukturalub Unia), jest wywoływany operator usuwania globalnego.For objects not of class type (class, struct, or union), the global delete operator is invoked. W przypadku obiektów typu klasy nazwa funkcji cofania alokacji jest rozpoznawana w zakresie globalnym, jeśli wyrażenie delete zaczyna się od jednoargumentowego operatora rozpoznawania zakresu ( ::
).For objects of class type, the name of the deallocation function is resolved in global scope if the delete expression begins with the unary scope resolution operator (::
). W przeciwnym razie operator delete wywołuje destruktor dla obiektu przed cofnięciem alokacji pamięci (jeśli wskaźnik nie ma wartości null).Otherwise, the delete operator invokes the destructor for an object prior to deallocating memory (if the pointer is not null). Operator delete można zdefiniować dla poszczególnych klas. Jeśli nie ma takiej definicji dla danej klasy, zostanie wywołany globalny operator delete.The delete operator can be defined on a per-class basis; if there is no such definition for a given class, the global operator delete is invoked. Jeśli wyrażenie delete służy do cofnięcia alokacji obiektu klasy, którego typ statyczny ma destruktor wirtualny, funkcja cofania alokacji jest rozwiązywana przez destruktor wirtualny typu dynamicznego obiektu.If the delete expression is used to deallocate a class object whose static type has a virtual destructor, the deallocation function is resolved through the virtual destructor of the dynamic type of the object.
Zobacz teżSee also
Wyrażenia z operatorami jednoargumentowymiExpressions with Unary Operators
SłużącKeywords
New i DELETE — operatorynew and delete Operators