ištrinti raktinį žodį C++

ištrinti yra an operatorius kad yra įpratęs sunaikinti masyvą ir ne masyvas (rodiklis) objektų kuriuos dinamiškai sukuria naujas operatorius.

    ištrinti galima naudojant bet kurį ištrinti operatorių arba ištrinti [ ] operatorių.
  • The naujas operatorius naudojamas dinaminiam atminties paskirstymui, kuris saugo kintamuosius krūvos atmintyje.
  • Tai reiškia, kad trynimo operatorius atima atmintį iš krūva .
  • Rodyklė į objektą nesunaikinama, sunaikinama žymeklio nurodyta reikšmė arba atminties blokas.
  • Ištrynimo operatorius turi tuštuma grąžinimo tipas, o tai reiškia, kad jis negrąžina jokios reikšmės.

Toliau pateikiami keli pavyzdžiai, kur galime pritaikyti trynimo operatorių:

1. Masyvo objektų ištrynimas

Masyvą ištriname naudodami [] skliaustus.

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. NULL žymeklio ištrynimas

NULL ištrynimas nesukelia jokių pakeitimų ir nesukelia klaidos.

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. Rodyklės su ar be reikšmės ištrynimas

Nurodyto žymeklio nurodyta atmintis bus atimta iš krūvos atminties.

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

Išvestis

Value of ptr1 = 0 Value of ptr2 = 20 

4. Tuščios rodyklės ištrynimas

Ištrynimo operatorius ne tik atlaisvina atmintį, bet ir iškviečia naikinamo objekto naikintoją. Štai kodėl, jei naudosime tuščią rodyklę su trynimu, tai sukels neapibrėžtą elgesį.

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

Išvestis

ptr deleted successfully 

5. Dinamiškai malloc() paskirtos atminties ištrynimas

Atminties, skirtos malloc() paskirstymas naudojant trynimo operatorių, taip pat lemia neapibrėžtą elgesį. Rekomenduojama naudoti delete for new ir free() 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;> }>

Išvestis

ptr2 deleted successfully 

Pastaba : Nors aukščiau pateikta programa puikiai veikia GCC. Nerekomenduojama naudoti trynimo su malloc().

6. Vartotojo nustatytų duomenų tipų kintamųjų ištrynimas

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

Išvestis

custom delete for size 1 custom delete for size 18 

Išimtys

1. Bandymas ištrinti ne žymeklio objektą

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

Išvestis

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

2. Bandymas ištrinti žymeklį į vietinį kamino priskirtą kintamąjį

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

Išvestis

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 

susiję straipsniai

  • naujas raktinis žodis
  • C++ malloc()