469,645 Members | 1,393 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

A question about allocating resources

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
Jul 23 '05 #1
3 1260
"Tony Johansson" <jo*****************@telia.com> wrote in message
news:qw*******************@newsb.telia.net...
: 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 23 '05 #2
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.

Jul 23 '05 #3
On Mon, 04 Apr 2005 08:02:30 GMT, "Tony Johansson"
<jo*****************@telia.com> wrote in comp.lang.c++:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Crirus | last post: by
4 posts views Thread by Tony Johansson | last post: by
9 posts views Thread by Gustaf | last post: by
15 posts views Thread by cedgington | last post: by
19 posts views Thread by allanallansson | last post: by
15 posts views Thread by sethukr | last post: by
27 posts views Thread by matt | last post: by
19 posts views Thread by =?ISO-8859-1?Q?Nordl=F6w?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.