Clustered Allocation

The operators

new()
and
delete()
are expensive.

When we talk about a method or an approach being expensive, we mean that this method uses a lot of resources, such as memory or CPU cycles. Using

new()
and
delete()
is expensive because they have a lot of overhead, and allocating memory does not happen quickly.

It is sometimes more efficient to grab a large area from

new()
and manage it yourself. Doing so cuts down on the number of times that
new()
and
delete()
are called. You grab a large area, and you don't call
new()
again until you use it up. If you use it up quickly you may want to grab twice as much next time.

Managing this pool of objects takes some overhead as well, however. You will have to decide if the cost of managing the pool of memory is greater than the cost of repeatedly calling

new()
. Generally, if you are creating a lot of objects, they are all of the same size and performance is critical, then this may be worth doing.

© 1998 by Wrox Press. All rights reserved.