A question about allocating resources

T

Tony Johansson

Hello experts!!

I reading in a book about C++ and there is something that I'm not sure
about. I don't belive that the book is wrong but
I will just ask you out there what you think.

The book says the following "Note that you cannot assume that all resources
are automatically released when the
entire program tetminates. While this is true for resources allocated
exclusively for
this program, such as internal memory, there are other resources obtained
from the
operating system, such as file locks or ports, that must be explicitly
released when the program terminates."

My question I thought that when a program terminates all resources no matter
if it's file lock, ports, memory or something else) is released
automatically when the program terminate. Is the book right or wrong?

Many thanks!

//Tony
 
I

Ivan Vecerina

: I reading in a book about C++ and there is something that I'm not sure
: about. I don't belive that the book is wrong but
: I will just ask you out there what you think.
:
: The book says the following "Note that you cannot assume that all
resources
: are automatically released when the
: entire program tetminates. While this is true for resources allocated
: exclusively for
: this program, such as internal memory, there are other resources obtained
: from the
: operating system, such as file locks or ports, that must be explicitly
: released when the program terminates."
:
: My question I thought that when a program terminates all resources no
matter
: if it's file lock, ports, memory or something else) is released
: automatically when the program terminate. Is the book right or wrong?

This is platform-specific, and not really a C++ question.

Most modern operating systems (Unices and the WinNT derivatives) reliably
dispose of all or most resources allocated by a client process that
has terminated. Windows versions up to Win98/Me were more likely to
display such a problem.

In any case, because you never know if your class/module will be used
as part of a larger application, a professional programmer will
always make sure that all resources acquired by a module are released.


Ivan
 
J

Jeremiah L.Jiang

Some resources will not be released if you don't release them
explicitly. And other applications will never use them. For example,
when you manipulate a scanner, you must open Data Source before you can
use it. But you must close Date Source after you finish scanning.
Otherwise, other applications can't use the scanner.
 
J

Jack Klein

Hello experts!!

I reading in a book about C++ and there is something that I'm not sure
about. I don't belive that the book is wrong but
I will just ask you out there what you think.

The book says the following "Note that you cannot assume that all resources
are automatically released when the
entire program tetminates. While this is true for resources allocated
exclusively for
this program, such as internal memory, there are other resources obtained
from the
operating system, such as file locks or ports, that must be explicitly
released when the program terminates."

My question I thought that when a program terminates all resources no matter
if it's file lock, ports, memory or something else) is released
automatically when the program terminate. Is the book right or wrong?

Many thanks!

//Tony

As others have said, some systems clean up better than others,
although there might always be some types of resources on any system
that might not be released until the next reboot or some such.

But you need to understand that this is not really a C++ language
question. The C++ language standard can only place requirements on
the actions of a correct conforming C++ program while it is executing.
It has no authority over your computer's operating system, and it
cannot in any way specify what your operating system does before or
after it runs a C++ program.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,663
Latest member
josh5959

Latest Threads

Top