23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

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.

Multi-way Merg<strong>in</strong>g................................<br />

669<br />

14.5<br />

Exercises..................................<br />

670<br />

java.datastructures.net<br />

14.1 Memory Management<br />

In order to implement any data structure on an actual computer, we need to use<br />

computer memory.Computer memory is simply a sequence of memory words, each of<br />

which usually consists of 4, 8, or 16 bytes (depend<strong>in</strong>g on the computer). These<br />

memory words are numbered from 0 to N − 1, where N is the number of memory<br />

words available to the computer. The number associated with each memory word is<br />

known as its address. Thus, the memory <strong>in</strong> a computer can be viewed as basically one<br />

giant array of memory words. Us<strong>in</strong>g this memory to construct data structures (<strong>and</strong> run<br />

programs) requires that we manage the computer's memory to provide the space<br />

needed for data—<strong>in</strong>clud<strong>in</strong>g variables, nodes, po<strong>in</strong>ters, arrays, <strong>and</strong> character str<strong>in</strong>gs—<br />

<strong>and</strong> the programs the computer is to run. We discuss the basics of memory<br />

management <strong>in</strong> this section.<br />

14.1.1 Stacks <strong>in</strong> the <strong>Java</strong> Virtual Mach<strong>in</strong>e<br />

A <strong>Java</strong> program is typically compiled <strong>in</strong>to a sequence of byte codes that are def<strong>in</strong>ed<br />

as "mach<strong>in</strong>e" <strong>in</strong>structions for a well-def<strong>in</strong>ed model—the <strong>Java</strong> Virtual Mach<strong>in</strong>e<br />

(JVM). The def<strong>in</strong>ition of the JVM is at the heart of the def<strong>in</strong>ition of the <strong>Java</strong><br />

language itself. By compil<strong>in</strong>g <strong>Java</strong> code <strong>in</strong>to the JVM byte codes, rather than the<br />

mach<strong>in</strong>e language of a specific CPU, a <strong>Java</strong> program can be run on any computer,<br />

such as a personal computer or a server, that has a program that can emulate the<br />

JVM. Interest<strong>in</strong>gly, the stack data structure plays a central role <strong>in</strong> the def<strong>in</strong>ition of<br />

the JVM.<br />

The <strong>Java</strong> Method Stack<br />

Stacks have an important application to the run-time environment of <strong>Java</strong><br />

programs. A runn<strong>in</strong>g <strong>Java</strong> program (more precisely, a runn<strong>in</strong>g <strong>Java</strong> thread) has a<br />

private stack, called the <strong>Java</strong> method stack or just <strong>Java</strong> stack for short, which is<br />

used to keep track of local variables <strong>and</strong> other important <strong>in</strong>formation on methods<br />

as they are <strong>in</strong>voked dur<strong>in</strong>g execution. (See Figure 14.1.)<br />

885

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

Saved successfully!

Ooh no, something went wrong!