The Problem
Memory allocation bugs (
malloc(3C)
bugs )
are among the most pernicious, difficult-to-locate
bugs in C programs.
There are four kinds of such bugs:
-
Never freeing a block of memory.
-
Using a block of memory after it has been freed.
-
Freeing a block of memory twice.
-
Referencing through an invalid pointer.
The first bug reveals itself when the program runs out of memory unexpectedly.
The other bugs reveal themselves when the program dumps core,
or even worse, doesn't
dump core, but produces an incorrect result.
In all four cases, the bug reveals
itself (if at all) at a moment in time long after
the bug actually occurred, and
at a place in the program far-removed from the actual location of the bug.
Finding memory allocation bugs involves painstaking,
error-prone games of pointer-chasing.
C++ allows the programmer to manage freestore
at a higher conceptual level than C.
Rather than allocating and deallocating blocks of raw memory,
in C++ the programmer
``news'' and ``deletes'' objects (instances of types).
However, C++ still admits
all the analogs of the four bugs listed above.
The effects of these bugs are the
same as in C, and are just as difficult to track down.
C++ also admits a new allocation
bug not found in C, that of specifying the wrong
size when deleting an array of objects.
Without a tool to assist the programmer,
managing the freestore would be just as
difficult in C++ as it is in C.
Next topic:
fs to the Rescue
Previous topic:
No More Memory Leaks - fs(3C++)
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004