usuń słowo kluczowe w C++

usuwanie jest operator to jest przyzwyczajone zniszczyć tablicę I nie-tablica (wskaźnik) obiekty które są dynamicznie tworzone przez nowy operator.

    usuwania można użyć za pomocą metody usuń operatora Lub usuń operatora [ ].
  • The nowy operator służy do dynamicznej alokacji pamięci, która przechowuje zmienne w pamięci sterty.
  • Oznacza to, że operator usuwania zwalnia pamięć z pliku sterta .
  • Wskaźnik do obiektu nie ulega zniszczeniu, wartość lub blok pamięci wskazywany przez wskaźnik ulega zniszczeniu.
  • Operator usuwania ma próżnia typ zwracany, co oznacza, że ​​nie zwraca żadnej wartości.

Poniżej znajduje się kilka przykładów zastosowań operatora usuwania:

1. Usuwanie obiektów szyku

Usuwamy tablicę za pomocą nawiasów [].

C++




// Program to illustrate deletion of array> #include> using> namespace> std;> int> main()> {> > // Allocate Heap memory> > int> * array => new> int> [10];> > // Deallocate Heap memory> > delete> [] array;> > return> 0;> }>

2. Usuwanie wskaźnika NULL

Usunięcie wartości NULL nie powoduje żadnych zmian i nie powoduje błędu.

C++




// C++ program for deleting> // NULLL pointer> #include> using> namespace> std;> int> main()> {> > // ptr is NULL pointer> > int> * ptr = NULL;> > // deleting ptr> > delete> ptr;> > return> 0;> }>

3. Usuwanie wskaźnika z wartością lub bez

Pamięć wskazywana przez określony wskaźnik zostanie zwolniona z pamięci sterty.

C++




// C++ program for deleting pointer with or without value> #include> using> namespace> std;> int> main()> {> > // Creating int pointer> > int> * ptr1 => new> int> ;> > // Initializing pointer with value 20> > int> * ptr2 => new> int> (20);> > cout < <> 'Value of ptr1 = '> < < *ptr1 < <> ' '> ;> > cout < <> 'Value of ptr2 = '> < < *ptr2 < <> ' '> ;> > // Destroying ptr1> > delete> ptr1;> > // Destroying ptr2> > delete> ptr2;> > return> 0;> }>

Wyjście

Value of ptr1 = 0 Value of ptr2 = 20 

4. Usuwanie wskaźnika pustki

Operator usuwania nie tylko zwalnia pamięć, ale także wywołuje destruktor obiektu, który ma zostać usunięty. Dlatego też, jeśli użyjemy wskaźnika void z usuwaniem, doprowadzi to do niezdefiniowanego zachowania.

C++




// C++ prgram for deleting a void pointer> #include> using> namespace> std;> int> main()> {> > // Creating void pointer> > void> * ptr;> > // Destroying void pointer> > delete> ptr;> > cout < <> 'ptr deleted successfully'> ;> > return> 0;> }>

Wyjście

ptr deleted successfully 

5. Usuwanie pamięci przydzielonej dynamicznie przez malloc()

Cofanie alokacji pamięci przydzielonej przez malloc() za pomocą operatora usuwania również prowadzi do niezdefiniowanego zachowania. Zalecane jest użycie usuwania dla nowych i free() dla malloc.

C++




// C++ program for deleting memory dynamically allocated by> // malloc> #include> using> namespace> std;> int> main()> {> > // Dynamic memory allocated by using malloc> > int> * ptr2 = (> int> *)> malloc> (> sizeof> (> int> ));> > delete> ptr2;> > cout < <> 'ptr2 deleted successfully'> ;> > return> 0;> }>

Wyjście

ptr2 deleted successfully 

Notatka : Chociaż powyższy program działa dobrze na GCC. Nie zaleca się używania usuwania z malloc().

6. Usuwanie zmiennych typów danych zdefiniowanych przez użytkownika

C++




// C++ program for deleting variables of User Defined data> // types> #include> using> namespace> std;> struct> P {> > // Overloading delete operator for single object> > // deallocation> > static> void> operator> delete> (> void> * ptr,> size_t> sz)> > {> > cout < <> 'custom delete for size '> < < sz < < endl;> > // ::operator delete(ptr) can also be used> > ::operator> delete> (ptr);> > }> > // Overloading delete operator for array deallocation> > static> void> operator> delete> [](> void> * ptr,> size_t> sz)> > {> > cout < <> 'custom delete for size '> < < sz < < endl;> > // ::operator delete(ptr) can also be used> > ::operator> delete> (ptr);> > }> };> int> main()> {> > P* var1 => new> P;> > delete> var1;> > P* var2 => new> P[10];> > delete> [] var2;> }>

Wyjście

custom delete for size 1 custom delete for size 18 

Wyjątki

1. Próba usunięcia obiektu niebędącego wskaźnikiem

C++




// C++ program for trying to delete a Non-pointer object> #include> using> namespace> std;> int> main()> {> > int> x;> > // Delete operator always> > // requires pointer as input> > delete> x;> > return> 0;> }>

Wyjście

error: type ‘int’ argument given to ‘delete’, expected pointer 

2. Próba usunięcia wskaźnika do zmiennej przydzielonej do stosu lokalnego

C++




// C++ program for trying to delete the pointer to a local> // stack-allocated variable> #include> using> namespace> std;> int> main()> {> > int> x;> > int> * ptr1 = &x;> > // x is present on stack frame as> > // local variable, only dynamically> > // allocated variables can be destroyed> > // using delete operator> > delete> ptr1;> > return> 0;> }>

Wyjście

main.cpp: In function ‘int main()’: main.cpp:16:12: warning: ‘void operator delete(void*, std::size_t)’ called on unallocated object ‘x’ [-Wfree-nonheap-object] 16 | delete ptr1; | ^~~~ main.cpp:9:9: note: declared here 9 | int x; | ^ free(): invalid pointer 

Powiązane artykuły

  • nowe słowo kluczowe
  • C++ malloc()