15.06.2013 Views

Dispensa in PDF - Dipartimento di Scienze Ambientali, Informatica e ...

Dispensa in PDF - Dipartimento di Scienze Ambientali, Informatica e ...

Dispensa in PDF - Dipartimento di Scienze Ambientali, Informatica e ...

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.

g(){<br />

.<br />

.<br />

.<br />

y = f(3, obj);<br />

.<br />

.<br />

.<br />

}<br />

f(a,b){<br />

<strong>in</strong>t c;<br />

<strong>in</strong>t d;<br />

MyClass e;<br />

c = d = 1;<br />

e = b;<br />

e.a1 = 7;<br />

return e.a1;<br />

}<br />

(a)<br />

Dynamic Cha<strong>in</strong><br />

Carnielli Efrem: Programmazione a componenti<br />

Valore <strong>di</strong> return: 7<br />

a 3<br />

b<br />

c ⊥ 1<br />

d ⊥ 1<br />

e NULL<br />

Activation record<br />

<strong>di</strong> f<br />

Activation record<br />

<strong>di</strong> g<br />

Activation Stack<br />

Figura 3.3. Esempi <strong>di</strong> chiamata <strong>di</strong> una funzione.<br />

Pr<strong>in</strong>cipi <strong>di</strong> progettazione<br />

a1 7<br />

a2 1<br />

Memoria Heap<br />

(a) = <strong>in</strong><strong>di</strong>rizzo <strong>di</strong> ritorno<br />

In questo esempio la funzione g <strong>in</strong> esecuzione (ve<strong>di</strong>amo che l’activation record <strong>di</strong> g<br />

è già presente nell’activation stack) chiama la funzione f. Di conseguenza viene<br />

<strong>in</strong>serito nell’activation stack l’activation record <strong>di</strong> f che presenta i campi dei<br />

parametri passati e delle variabili. Il passaggio dei parametri viene effettuato per<br />

valore, qu<strong>in</strong><strong>di</strong> ve<strong>di</strong>amo che b assume il puntatore all’oggetto nella memoria Heap<br />

(frecce <strong>in</strong> blu). L’activation record memorizza anche l’<strong>in</strong><strong>di</strong>rizzo dell’istruzione<br />

successiva alla chiamata <strong>di</strong> f <strong>in</strong> g, ovvero y =valore <strong>di</strong> ritorno .<br />

Con l’esecuzione <strong>di</strong> f vengono mo<strong>di</strong>ficati i campi dell’activation record <strong>di</strong> f, <strong>in</strong><br />

particolare viene mo<strong>di</strong>ficato il valore <strong>di</strong> c e d, e punta all’oggetto puntato da b,<br />

viene mo<strong>di</strong>ficato un campo dell’oggetto e impostato il valore da ritornare a g (tutto<br />

visualizzato <strong>in</strong> rosso).<br />

Un altro importante elemento che potrebbe essere presente all'<strong>in</strong>terno<br />

dell'activation record a seconda del tipo <strong>di</strong> l<strong>in</strong>guaggio <strong>di</strong> programmazione che<br />

utilizziamo è lo static l<strong>in</strong>k.<br />

Il l<strong>in</strong>k statico che da orig<strong>in</strong>e alla static cha<strong>in</strong> è un puntatore al contesto della<br />

funzione, ovvero tutto ciò che viene utilizzato dalla funzione ma è locale ad essa;<br />

53

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

Saved successfully!

Ooh no, something went wrong!