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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

ecursive <strong>in</strong>vocations, <strong>and</strong> thus the run-time stack, only grows up to size n,<br />

because call<strong>in</strong>g factorial(1) returns 1 immediately without <strong>in</strong>vok<strong>in</strong>g itself<br />

recursively. The run-time stack allows for method factorial() to exist<br />

simultaneously <strong>in</strong> several active frames (as many as n at some po<strong>in</strong>t). Each frame<br />

stores the value of its parameter n as well as the value to be returned. Eventually,<br />

when the first recursive call term<strong>in</strong>ates, it returns (n − 1)!, which is then<br />

multiplied by n to compute n! for the orig<strong>in</strong>al call of the factorial method.<br />

14.1.2 Allocat<strong>in</strong>g Space <strong>in</strong> the Memory Heap<br />

We have already discussed (<strong>in</strong> Section 14.1.1) how the <strong>Java</strong> Virtual Mach<strong>in</strong>e<br />

allocates a method's local variables <strong>in</strong> that method's frame on the <strong>Java</strong> run-time<br />

stack. The <strong>Java</strong> stack is not the only k<strong>in</strong>d of memory available for program data <strong>in</strong><br />

<strong>Java</strong>, however.<br />

Dynamic Memory Allocation<br />

Memory for an object can also be allocated dynamically dur<strong>in</strong>g a method's<br />

execution, by hav<strong>in</strong>g that method utilize the special new operator built <strong>in</strong>to <strong>Java</strong>.<br />

For example, the follow<strong>in</strong>g <strong>Java</strong> statement creates an array of <strong>in</strong>tegers whose size<br />

is given by the value of variable k:<br />

<strong>in</strong>t[] items = new <strong>in</strong>t[k];<br />

The size of the array above is known only at runtime. Moreover, the array may<br />

cont<strong>in</strong>ue to exist even after the method that created it term<strong>in</strong>ates. Thus, the<br />

memory for this array cannot be allocated on the <strong>Java</strong> stack.<br />

The Memory Heap<br />

Instead of us<strong>in</strong>g the <strong>Java</strong> stack for this object's memory, <strong>Java</strong> uses memory from<br />

another area of storage—the memory heap (which should not be confused with<br />

the "heap" data structure presented <strong>in</strong> Chapter 8). We illustrate this memory area,<br />

together with the other memory areas, <strong>in</strong> a <strong>Java</strong> Virtual Mach<strong>in</strong>e <strong>in</strong> Figure 14.2.<br />

The storage available <strong>in</strong> the memory heap is divided <strong>in</strong>to blocks, which are<br />

contiguous array-like "chunks" of memory that may be of variable or fixed sizes.<br />

To simplify the discussion, let us assume that blocks <strong>in</strong> the memory heap are of a<br />

fixed size, say, 1,024 bytes, <strong>and</strong> that one block is big enough for any object we<br />

might want to create. (Efficiently h<strong>and</strong>l<strong>in</strong>g the more general case is actually an<br />

<strong>in</strong>terest<strong>in</strong>g research problem.)<br />

Figure 14.2: A schematic view of the layout of<br />

memory addresses <strong>in</strong> the <strong>Java</strong> Virtual Mach<strong>in</strong>e.<br />

890

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

Saved successfully!

Ooh no, something went wrong!