[comp.lang.pascal] Absolute troubles

ajayshah@aludra.usc.edu (Ajay Shah) (01/23/90)

Consider this code:

-------------------------Start
type
	VectorType = array[1..10] of real;

procedure xxx(var theta:VectorType);
var
	sigma : real absolute theta;
	beta : VectorType absolute theta[2];
begin
end;
-------------------------End

I'm trying to pass a stacked vector and reference it as two distinct
vectors internally (instead of having code like theta[i+1] to
reference subscript i of beta).  He doesn't let me do this (tp5.5,
i.e.).  The declaration for sigma is fine, the declaration for beta is 
not.

What am i missing?  Is there a fix on the lines of

-------------------------Start
var
	sigma : real absolute theta;
	beta : ^VectorType;
begin
	beta := ptr(theta[2])
end;

-------------------------End

The code above may not be syntactically immaculate (i'm not fluent
with such C-style dirty pointer manipulations). The basic idea is to
declare beta as a pointer, never new it, and just set it to point to
theta[2] and use it as a VectorType.

Ideas on this?

_______________________________________________________________________________
Ajay Shah, (213)747-9991, ajayshah@usc.edu
                              The more things change, the more they stay insane.
_______________________________________________________________________________

dhinds@portia.Stanford.EDU (David Hinds) (01/23/90)

In article <7559@chaph.usc.edu>, ajayshah@aludra.usc.edu (Ajay Shah) writes:
> type
> 	VectorType = array[1..10] of real;
> 
> procedure xxx(var theta:VectorType);
> var
> 	sigma : real absolute theta;
> 	beta : VectorType absolute theta[2];

    The 'sigma' declaration apparantly works, but the 'beta' declaration
fails.  Actually, I'm surprised either of them works.  You are asking
the compiler to give a variable the same address as a parameter passed
by reference, which inherantly means that the address is not defined at
compile time.  The compiler might not catch it as an error, but if I
were you I would try running some kind of test program with just a
'sigma'-like declaration, to see if it ACTUALLY works.  You definitely
can't get away with the theta[2] declaration.
    If a 'sigma'-like declaration really can be handled properly by the
compiler, you could instead declare a record with two fields, called
'sigma' and 'beta', of the appropriate types, 'absolute theta'.  The
subfields of the record would then correctly refer to the parts of the
passed vector. i.e.,

   procedure xxx(var theta: VectorType);
   type 
     thetatype = record
       sigma: real;
       beta: VectorType;
     end;
   var
     y: thetatype absolute theta;

and use 'y.sigma' and 'y.beta' as the parts of 'theta'.  I have a bad
feeling about the use of 'absolute' with parameters, though.  At worst,
you could declare:

     y: ^thetatype;
  begin
     y := @theta;

and refer to 'y^.sigma' and 'y^.beta' as your vector parts.

 - David Hinds
   dhinds@portia.stanford.edu

dmurdoch@watstat.waterloo.edu (Duncan Murdoch) (01/23/90)

In article <8470@portia.Stanford.EDU> dhinds@portia.Stanford.EDU (David Hinds) writes:
>fails.  Actually, I'm surprised either of them works.  You are asking
>the compiler to give a variable the same address as a parameter passed
>by reference, which inherantly means that the address is not defined at
>compile time.  

Declaring something at the same address as a parameter using absolute is
fine.  On a PC, parameters are addressed at fixed offsets relative to the BP 
register, so the addresses (offsets) are known at compile time.  

Duncan Murdoch