Item 16 - Use the same form in corresponding uses of new and delete.
What's wrong with this picture?
Here, the new
is matched with a delete
, Still, something is quite wrong. 99 of the 100 string
objects pointer to by stringArray
are unlikely to be properly destroyed, because their destructors will probably never be called.
Background process of new
: When you employ a new expression (i.e., dynamic creation of an object via a use of new
), two things happen.
memory is allocated (via function named operator
new
)One or more constructors are called for that memory.
Background process of delete
: When you employ a delete expression (i.e., use delete
), two things happen
one or more destructors are called for the memory.
memory is deallocated (via function named operator
delete
)
Example:
The rule is simple: if you use []
in a new
expression, you must use []
in the corresponding delete
expression. If you don't use []
in a new
expression, don't use []
in the matching delete
expression.
This rule is also noteworthy for the typedef
-inclined, because it means that a typedef
's author must document which form of delete
should be employed when new
is used to conjure up objects of the typedef
type. For example, consider this typedef
:
Because AddressLines
is an array, this use of new
,
must be matched with the array form of delete
Things to Remember
if you use[] in a
new
expression, you must use[]
in the correspondingdelete
expression. If you don't use[]
in anew
expression, you must not use[]
in the correspondingdelete
expression.
Last updated