What is the benefit of destructor?

In c++ books, they usually don't elaborate about the hardware stuff. For example, the destructor invokes when the object is destroyed. What is the benefit of using it? Is it a hardware stuff I mean the destructor free the specific area of memory that is assigned to the object, then after using the object, we want to free the memory for other purposes. And even if this so, why we are really concerned to free the memory since if we close the program, everything is gone. Am I right? I believe that everything has real purpose, however, no one tells you until you ask. Don't get me wrong, I understand how to use the destructor but not Why?

thanks in advance.
It is just good practice to free up memory. There is not much emphasis on it now because most compiled programs do the freeing up automatically for you. But back in the good ol' days of computing, you had to manually free the memory by tell the program to do so; otherwise...well you don't want to find out what happens otherwise.
It's not just good practice. If you run any program which has a constant memory leak long enough it will crash. C++ doesn't have a garbage collection mechanism. If you don't free a raw pointer before its lifetime ends the memory it was pointing to will be lost but flagged as "in use" until the program ends. C++ programs don't just "free the memory automatically".

There isn't as much emphasis on it these days because most of the raw pointer stuff is encapsulated inside things such as std::string and std::vector, or if we need pointers we can use references or smart pointers. Anything you write which deals with raw pointers needs to delete allocated memory eventually (manually by using delete ptr;).
Topic archived. No new replies allowed.