28.06.2013 Views

ISO Pascal reference manual

ISO Pascal reference manual

ISO Pascal reference manual

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.

END;<br />

LocaIFNC = rip.; Ide<strong>reference</strong> for F77 result)<br />

Using <strong>Pascal</strong> with other languages<br />

(N.B. As described in section 6.2.2 it is necessary to compile this code with<br />

assignment checks disabled, otherwise the <strong>reference</strong> to the Fortran result<br />

variable via a <strong>Pascal</strong> pointer will probably fail since the result variable will<br />

not have been allocated on the <strong>Pascal</strong> heap.)<br />

This is the obvious logical method of interfacing the procedure, but is by no<br />

means the most efficient way of calling a Fortran function! The main<br />

problem is that the only (legal) way of using pointers in standard <strong>Pascal</strong> is<br />

in claiming, accessing and disposing of heap memory via NEW, DISPOSE<br />

etc. - it is not possible to point pointers at normal local variables. In the<br />

above example some improvement would obtain by specifying FNC as<br />

taking a VAR parameter of type FNCParams, rather than a pointer to such<br />

a structure - VAR parameters are implemented by the same mechanism in<br />

low-level terms. Thus:<br />

IMPORT FUNCTION FNC (VAR p: FNCParams): IntRef;<br />

PROCEDURE LocaIFNC (n1, n2: Integer): Integer;<br />

VAR<br />

rp: IntRef;<br />

paramBlock: FNCParams;<br />

BEGIN<br />

WITH paramBlock DO BEGIN<br />

NEW (p1); NEW (p2);<br />

p1l := n1; p21 := n2;<br />

rp := FNC (paramBlock);<br />

DISPOSE (p1); DISPOSE (p2)<br />

END;<br />

LocalFNC = rpt;<br />

END;<br />

If the function is to be called more than once, it might be worthwhile<br />

removing the calls on NEW and DISPOSE from the procedure, since these<br />

involve an amount of heap management activity. This could be done by<br />

making the parameter block a global variable, and claiming the space for<br />

the parameters (via p1 and p2) only once, before the first call on LocalFNC<br />

was made. Thus we would arrive at:<br />

<strong>Pascal</strong> Issue 1 43

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

Saved successfully!

Ooh no, something went wrong!