On 21/12/2021 23:08, Bakul Shah wrote:
> Another question for Algol68 experts in this group:
> A long time ago in this newsgroup
Wow! A thread from more than a third of a century ago that
we both contributed to!
> Piet Von Oostrum said this:
> One example: dynamic arrays are easy: Algol 60 had it, see also
> the discussion on alloca in this newsgroup. Unions are easy: C
> has them. The combination of dynamic arrays and union however
> forces you to use the heap, and hence garbage collection.
I note Charles's reply, but I don't think it's directly to
do with /dynamic/ arrays, assuming that by that you mean arrays
whose size is not known at compile time. "Simply" allocating space
for the largest possible member of the union is not quite so simple,
in general, ISTM. Consider, eg:
 REAL a; # 2^16, so "only" something like 256K bytes #
 UNION (INT,  REAL) b;
Now we don't know what size of array will be assigned to any one of
the elements of "b", but it could be "a", so if we allocate the amount
of storage that /could/ be used by any element of "b", then we have to
allow space for 2^16 reals for each, or space for 2^32 reals in total.
That will certainly overflow in a 32-bit computer, even if in reality
only tiny arrays are used in "b". So the choice for the compiler is
to put artificial constraints on the sizes of "a" and "b", or else to
arrange to allocate only the space that is actually used. But that
is not known at compile time, even if, as above, we know the sizes of
all the arrays that occur. So, as a matter of practicality, we have
to use the heap so that elements of "b" can be re-sized as needed.
Or, of course, we could change the union to include a "REF  INT".
Note that in the comparable case with "STRUCT" types, the
type includes the bounds [eg "MODE STRUCT S = (REAL a,  INT b);"]
so the actual sizes are known when objects are declared.
IOW, whereas in C a union is an overlaid structure, in A68
it is simply a name for a collection of types. There are no actual
objects whose type is "UNION ...".
Meanwhile, I note that in your reply to Charles, you say
that you "never had a chance to use A68". Well, now you do! A68G
is freely available, and works "everywhere". As Bart is fond of
pointing out, it's not the fastest kid on the block; but it's
absurdly fast compared with the mainframes of the '70s, so it's
plenty fast enough for everything I need to do.
Andy Walker, Nottingham.
Andy's music pages: www.cuboid.me.uk/andy/Music
Composer of the day: www.cuboid.me.uk/andy/Music/Composers/Nevin