11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

422 Chap. 12 Lists <strong>and</strong> Arrays RevisitedH<strong>and</strong>leMemory BlockFigure 12.15 Using h<strong>and</strong>les for dynamic memory management. The memorymanager returns the address of the h<strong>and</strong>le in response to a memory request. Theh<strong>and</strong>le stores the address of the actual memory block. In this way, the memoryblock might be moved (with its address upd<strong>at</strong>ed in the h<strong>and</strong>le) without disruptingthe applic<strong>at</strong>ion program.memory manager, it might be possible to get more memory from the system-levelnew oper<strong>at</strong>or, such as is done by the freelist of Section 4.1.2.The last failure policy th<strong>at</strong> we will consider is garbage collection. Considerthe following series of st<strong>at</strong>ements.int[] p = new int[5];int[] q = new int[10];p = q;While in Java this would be no problem (due to autom<strong>at</strong>ic garbage collection), inlanguages such as C++, this would be considered bad form because the originalspace alloc<strong>at</strong>ed to p is lost as a result of the third assignment. This space cannotbe used again by the program. Such lost memory is referred to as garbage, alsoknown as a memory leak. When no program variable points to a block of space,no future access to th<strong>at</strong> space is possible. Of course, if another variable had firstbeen assigned to point to p’s space, then reassigning p would not cre<strong>at</strong>e garbage.Some programming languages take a different view towards garbage. In particular,the LISP programming language uses the multilist represent<strong>at</strong>ion of Figure12.5, <strong>and</strong> all storage is in the form either of internal nodes with two pointersor <strong>at</strong>oms. Figure 12.16 shows a typical collection of LISP structures, headed byvariables named A, B, <strong>and</strong> C, along with a freelist.In LISP, list objects are constantly being put together in various ways as temporaryvariables, <strong>and</strong> then all reference to them is lost when the object is no longerneeded. Thus, garbage is normal in LISP, <strong>and</strong> in fact cannot be avoided duringroutine program behavior. When LISP runs out of memory, it resorts to a garbagecollection process to recover the space tied up in garbage. Garbage collection consistsof examining the managed memory pool to determine which parts are stillbeing used <strong>and</strong> which parts are garbage. In particular, a list is kept of all programvariables, <strong>and</strong> any memory loc<strong>at</strong>ions not reachable from one of these variables areconsidered to be garbage. When the garbage collector executes, all unused memoryloc<strong>at</strong>ions are placed in free store for future access. This approach has the advantageth<strong>at</strong> it allows for easy collection of garbage. It has the disadvantage, from a user’s

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!