When to use a garbage collector?

C

Carlo Milanesi

Krice ha scritto:
Yannick Tremblay kirjoitti:

All these examples where memory recovery is needed seem to be
anything else than normal application running in PC, so I guess
it's good to have exceptions when they have some kind of real
function, but I think it's a waste of time to write such code in
PC application, because the construction never(?) fails, unless
you run out of memory.

What do you mean with "some kind of real function"?

Even in PC applications exceptions are very useful.
Try to create a 99999x99999 color bitmap using Windows Paint
(mspaint.exe) and you will get an error message, but the application
will continue to run. Would you prefer a crash?
 
J

Jerry Coffin

Yannick Tremblay kirjoitti:

All these examples where memory recovery is needed seem to be
anything else than normal application running in PC, so I guess
it's good to have exceptions when they have some kind of real
function, but I think it's a waste of time to write such code in
PC application, because the construction never(?) fails, unless
you run out of memory.

Construction can fail for _lots_ of reasons other than running out of
memory. In networking, for example, you might have an object to
represent a connection to a server. If you can't connect to the server,
you can't create the connection object. OTOH, that hardly implies that
the program must fail -- you might easily have a number of servers to
try, and as long as you can connect to any one of them, your program can
continue to run perfectly well.
 
R

Richard Herring

Stefan Ram said:
A function should not be coupled to an application more than
necessary, so that the function might be used in a library as well
(or one might even write functions for use in a library).

Usually, a function does not know which user interface the
program calling it employs. ›::std::cout‹ might be associated
with a console, or it might not be, when the application is GUI
based, web based or a driver or a service without a user interface.

So what should ›print out‹ mean in the general case?
Should the function use ›::std::cout << ...‹ or what else to
›print out‹ the most meaningful error message it can?

It should delegate that decision to your chosen logging package.
 
M

Matthias Buelow

Carlo said:
Even in PC applications exceptions are very useful.
Try to create a 99999x99999 color bitmap using Windows Paint
(mspaint.exe) and you will get an error message, but the application
will continue to run. Would you prefer a crash?

Guarding against invalid user input is not a good use case for
exceptions. Erroneous user input is hardly exceptional, is it?
 
N

Noah Roberts

Matthias said:
Guarding against invalid user input is not a good use case for
exceptions. Erroneous user input is hardly exceptional, is it?

Depends on the situation really. The input that the user has entered in
this case is actually valid, in that it is a real size, but the error is
more likely caused by inability to allocate the necessary memory for
such a huge bitmap.

I'd call that exceptional even if it is ultimately caused by a stupid user.
 
C

Carlo Milanesi

Matthias Buelow ha scritto:
Guarding against invalid user input is not a good use case for
exceptions. Erroneous user input is hardly exceptional, is it?

It not an invalid input. In this program you can specify a width from 1
to 99999 pixels and a height from 1 to 99999 pixels.
For example, I can create a 10x99999 bitmap or a 99999x10 bitmap.
This program appears to allocate about 6 to 10 bytes for every pixel.
Perhaps, if your computer can allocate 60 GB of virtual memory for a
single process you can create such a bitmap.
It would be silly to limit the memory of the bitmap, as different users
may have different memory.
I think that raising an exception is the best way to handle this kind
user input.
 
A

Alisha

For scarce resources, like resource handles and large memory blocks, use
reference counting. For routine memory allocations, use gc.


Using gc effectively entails using different coding/design techniques,
so it is not practical to try to shift the design after the fact.

What is the work of Garbage collector?
 

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
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top