+ 3
What is the purpose of delete operator?
The delete operator destroy the object created with new by deallocating the memory associating with the object.
3 Réponses
+ 1
AFAIK this way you can free up memory meaning that your object no longer takes up memory since it's destroyed. It's useful for bigger projects where memory management is important so the app doesn't freeze/works more efficiently.
- 1
The delete operator destroy the object created with new by deallocating the memory associating with the object.
- 1
it is to avoid leaks of memory.
When you allocate memory for your variable it keeps on memory regardless of the scope{}, think on a while loop and you allocate an array of 100 bytes once each loop, then if you don't deallocate memory it will be full fill and something rare will happen or the program will break. In embedded sistem it usually is the reason of problems.
Some languages like Java implements automatically garbage collector, in c++ you have to do manually.
In c++ you have the option to use intelligence pointers(unique_ptr, shared_ptr) that performance the deallocation automatically, but sometimes pointers can't be changed by intelligence pointers.