"Greg" <gr****@pacbell.net> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
:
bo*@coolgroups.com wrote:
: > why can't there be a deleteall keyword in c++ that
: > deletes all the memory previously allocated by
: > the new keyword?
: >
: > wouldn't this solve a lot of memory leak issues?
:
: If a program makes a series of memory allocations of equal size, then
: it can use a memory "pool" (such as boost's "pool" library) so that all
: of the allocations are fulfilled from the same block of reserved
: memory.
NB: there is no need for all objects in the pool to be of equal size.
: A program can then deallocate the entire pool and thereby free
: all of the pooled allocations at once. And indeed this approach is not
: only very efficient but also eliminates the chance that any of pooled
: allocations could leak.
:
: Unfortunately, relatively few applications actually have memory
: allocation patterns that could be implemented by a pool. Instead most
: programs create and delete objects in such a way that their life spans
: overlap; as a consequence, a typical program is constantly allocating
: and deallocating memory in various sizes and without adhering to any
: discernable pattern. Therefore there is never a point in most programs'
: execution in which all allocations up to that point could safely be
: freed at once. So while your idea certainly has merit, it is applicable
: only to relatively few programs.
Well: pooled allocations were an integral part of the Objective C
library on NeXT and MacOSX, and I think the feature is well taken
advantage of.
For example, a web browser could have one pool per rendered document.
Several apps could have one pool per document.
The Apache web server also heavily uses a memory pooling system,
they have some C libraries to support that.
Also, any function that allocates temporary data as part of its
processing could use a memory pool. Although, in C++, we tend to
use RAII and stack-based objects instead.
But another issue that needs to be addressed when using memory
pools (just as for garbage collection), is how to deal with
destructors that may have to be called (to release non-memory
resources). This typically requires some system to register
destructors, and ends up being more complex than classic RAII
with stack-based objects.
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <>
http://www.brainbench.com