new operator allocates memory for objects. How is this memory deallocated?
deleteoperator to match the
newoperator. The reason for this is simple: It's not necessary!
The first statement allocates memory for an
d = new Array (50); d = null;
Arrayobject large enough to hold 50 elements, and then records the fact that the new object is referenced by a variable named d.
The next statement, however, changes d so that instead of referring to the
Array object, it instead contains the special value
null. Since there is no longer any way to access the
Reclaiming memory allocated for objects which can no longer be accessed is called garbage collection. Allocated objects which are no longer accessible by variables are called orphan objects.
This next example shows how to modify the above code to prevent the destruction of the
d = new Array (50);make e refer to the same object as d
e = d; //
disassociate d from the
d = null; //
Arrayobject is referenced by e at the moment it is abandoned by d, the
Arrayobject remains intact.
null works pretty well for this.)
11 Jan. 1997