Portability questions (Arrays)_(re)

Sun, 1 Jun 1997 19:44:33 +0200 (MET DST)


According to Frank Heckenbach:
> 
> I'm briefly describing the situation I have. It's about speed optimization.
> [...]
> I thought about optimizing it with a pointer like this:
> 
> function f(var a:t):integer;
> var
>   p:^integer;
> begin
>   p:=@a[0];
>   {some loop}
>     if {...} then
>       begin
>         p^:=...;
>         inc(p) {is this ok, BTW?}
>       end;
>   {...}
>   f:="offset of p in a"
> end.

"inc(p)" should work in GPC.  You can even do

    for p:= @a [ 0 ] to @a [ n ] do
      p^:= ...;

Since all this is a "not very Pascalish" feature of GPC, you must enable
it with the (*$X+*) directive.

> So "f:=(integer(p)-integer(@a)) div sizeof(a[0])" doesn't seem to work then.
> Or are other means of pointer arithmetic available -- or completely other
> ways to do it?

This wouldn't work in C either because of the same reason (SizeOf ( Pointer )
needn't be the same as SizeOf ( Integer )).  How would you solve this problem
in C?

What about intoducing a "difference between pointers" operator which returns
the number of elements they lie apart (as an integer)?

    f:= p - @a [ 0 ];

(Only with (*$X+*), of course.)

Greetings,

    Peter

  Dipl.-Phys. Peter Gerwinski, Essen, Germany, free physicist and programmer
peter.gerwinski@uni-essen.de - http://home.pages.de/~peter.gerwinski/ [970201]
 maintainer GNU Pascal [970510] - http://home.pages.de/~gnu-pascal/ [970125]


Peter Gerwinski (peter@agnes.dida.physik.uni-essen.de)

HTML conversion by Lluís de Yzaguirre i Maura
Institut de Lingüística Aplicada - Universitat "Pompeu Fabra"
e-mail: de_yza@upf.es