Robert said:
You could try setjmp() and longjmp(), but that won't clear dynamically
allocated storage.
s/restart/fix
s/sometimes/once
The counsel of perfection is unassailable, as far as it
goes -- but only that far. Programming is largely an economic
activity: You write (and enhance, and fix, and rewrite ...) a
program to accomplish some purpose or other, the idea being
that the effort expended on the program will be less than it
would have taken to accomplish the same purpose without the
program's aid.
... which means that the decision to spend further effort
fixing it or to spend less effort on a sloppy work-around is
also an economic decision. It calls for a certain amount of
guesswork about the likely future use(s) of the program, too:
How much will it hurt if I use the work-around today and then
wind up needing to do the full-scale fix later anyhow? But it's
not a law of the universe that every bug must be squeezed out
of every program! Sometimes the expedient course, and even the
sensible course, is to use the program as it stands, bugs and
all, and to tolerate those that you must.
And even if you find this attitude defeatist and perhaps
loathsome, consider: The fact that a program consumes more and
more memory as time passes and eventually exhausts it is not
proof of a memory leak. The key word is "fragmentation," and
in TAOCP section 2.5 Knuth reports on a proof by J.M. Robson
that *all* allocation methods are vulnerable to fragmentation
unless they are able to move allocated blocks: "[...] even when
blocks are restricted to be of sizes 1 and 2, overflow might
occur with memory only about 2/3 full, no matter what allocation
algorithm is used!"