Well, you "are right" , that is when your
application exits, the memory and other resources are automatically freed
by the operating system. However, this is NOT the important point....
Generally, in C++ you should dispose of memory you've allocated once you've
performed the operations for which you allocated it, that way you can reduce
the "high water mark" enabling you to handle larger amounts of data without
running out of virtual memory. Consider the situation such as a GUI like
MS
Word or Internet Exploder , you potentially could open, edit and close
multiple documents and have
the same application running for several days at a time. If memory wasn't
properly deallocated
once a document had been closed ( for instance), the app would eventually
consume all the virtual memory on the machine and would eventually crash, do
undefined things,
etc. Even with a simple "batch" type application, it is important to
identify the points in
the flow of code where allocated memory can be safely disposed, this way the
application
could potentially handle a larger data set with the same physical/virtual
resources available
on the computer.
Of course, if your app is sooooo simple that it just spits out some output
and quits, then this
is probably not an issue, although I would learn the habit of correctly
managing memory as
a professional skill to do as a matter of routine so you don't have to
revisit the code in the
future.
Threads have nothing to do with memory management (allocation/deallocation).
Memory can be allocated by one thread and deleted by another thread, there
are no requirements regarding disposing
of memory, you have to program that into your code. As a general rule in a
C++ program, wherever
you allocate memory using new ( or home brewed memory allocator), there must
be a corresponding
delete(s) somewhere to deallocate it, if you don't got that, you got a
memory leak. Whenever I see a
memory allocation such as malloc or new, I want to assure myself that there
is a correct delete/free
operation happening in the normal flow of control. Better still, be sure
there's a correct delete/free in
the C++ exception flow. Constructors/destructors are very handy for
correctly & automatically managing memory allocation/deallocation in C++
programs. Poor guys like me, had to do this
by pure brain-power before C++ came along, with C++ you've got it easy!
Tools such as Purify can help identify memory leaks after the fact, but in
my opinion its far
easier to bake the memory management into the original code. I'd add that
I've encountered a number of
"killer" bugs in production applications which were due to memory leaks, so
get into the
habit of correct memory management as a career advancement strategy. When
you allocate, figure
out where you need to deallocate.
I don't know which book you can look at, this is nothing to do with threads,
any good book
on C++ should drill in "initialization is resource allocation" and go from
there.
dave