delete (C++)

From Seo Wiki - Search Engine Optimization and Programming Languages
Jump to navigationJump to search

In the C++ programming language, the delete operator returns memory allocated by new back to the heap. A call to delete must be made for every call to new to avoid a memory leak. After calling delete the memory object pointed to is invalid and should no longer be used. Many programmers assign 0 (null pointer) to pointers after using delete to help minimize programming errors. Note, however, that deleting a null pointer has no effect, so it is not necessary to check for a null pointer before calling delete.

Example code snippet:

int *p_var = 0;     // new pointer declared
p_var = new int;       // memory dynamically allocated
 
/* .......
other code
........*/

delete p_var;          // memory freed up
p_var = 0;          // pointer changed to 0 (null pointer)

Arrays allocated with new [] can be similarly deallocated with delete []:

int size = 10;
int *p_var = 0;     // new pointer declared
p_var = new int [size];// memory dynamically allocated

/* .......
other code
........*/

delete [] p_var;       // memory freed up
p_var = 0;          // pointer changed to 0

ru:Delete (C++)

If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...