Portability questions (Arrays)_(re)

Sat, 3 May 1997 23:44:44 +0200 (MET DST)


According to Frank Heckenbach:
> 
> 1. Is it safe to assume -- for all platforms, including possible future
>    ones -- that SizeOf(a) = ((Ord(y)-Ord(x)+1) * SizeOf(t)?

I first thought that this would not be the case due to alignment
problems, but ...:

That's *exactly* the way how GPC calculates the size of an array.

Why is this so, e.g. with an array of something which has 3 bytes?


    Program Test;

    Type
      r = packed record
        x: ShortWord;
        y: Byte;
      end (* r *);

    Var
      x: packed array [ 1..1000 ] of r;

    begin
      writeln ( SizeOf ( x ) );
      writeln ( SizeOf ( x [ 1 ] ) );
    end.


The output is 4000 and ... 4.  To avoid alignment problems in an "array
of r", the record `r' has size 4, not 3.

I conclude that the assumption is safe.

> 2. Is it safe to assume that
>    Integer(@a[p])-Integer(@a[q]) = (p-q) * SizeOf(t)?

Yes, for the same reason.  If the "natural" size of `t' would cause
alignment problems, `t' is blown up by GPC.

>    (Is it generally allowed to cast a pointer into an integer,
>    in the first place?)

I think so.  The GNU coding standard says about GCC's (and thus GPC's)
behaviour:

    You can assume that all pointers have the same format, regardless
    of the type they point to, and that this is really an integer.

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 [970420] - 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