[comp.lang.pascal] Variable sized arrays?

amead@s.psych.uiuc.edu (alan mead) (04/22/91)

wct@po.CWRU.Edu (William C. Thompson) writes:


>I don't suppose there is ANY way possible to have a variable
>sized array, is there.  Maybe in 6.0?  I need to save a

Where there's a will there's a way.  In fact, I just posted about it.
You need to declare a type array of ___[1..<the biggest index you'd
need>] and then declare a pointer to it.  You can then use GetMem() to
allocate exactly enough memory (ie, what makes this technique dynamic
is that GetMem decides how much memory to use).

I think GetMem() requires that 64K or less is requested--if you want
more, you have to turn off range checking AND request multiple 64K
*contiguous* chunks (I think--I'm not sure if it's always an error to
use an index greater than 65535).

Check out my post if it's still around.  It's titled "need array on heap" 
or something (or else I'll send it by email).

-alan 

DBH106@psuvm.psu.edu (04/22/91)

Just make a linked list.  Then it is simple to access th Nth element in the
list.

function varray (list : listtype; n : integer) : nodetype;
var
  temp : pointertype;
  k : integer;
begin
  new(temp);
  temp := list;
  for k := 2 to n do
    temp := temp.next;
  varray := temp.data;
end;

Hope this helps.  In my Comp Sci class, we implemented a linked list.  I have a
Unit that I think is cool.  It includes this function (or something very
similar.  If anyone is interested, I may post it after finals in three weeks.

Dan Harter
DBH106@psuvm.psu.edu

kushmer@bnlux1.bnl.gov (christopher kushmerick) (04/22/91)

In article <1991Apr21.211234.17738@ux1.cso.uiuc.edu> amead@s.psych.uiuc.edu (alan mead) writes:
>wct@po.CWRU.Edu (William C. Thompson) writes:
>I think GetMem() requires that 64K or less is requested--if you want
>more, you have to turn off range checking AND request multiple 64K
>*contiguous* chunks (I think--I'm not sure if it's always an error to

How is one supposed to allocate contiguous blocks?

Plus I doubt that this would work anyway, because the limitation is the
size of a segment, minus a few bytes used by the heap manager. Thus
you could not allocate contiguous blocks, because the blocks would be 
separated by these housekeeping bytes.

-- 
Chris Kushmerick                                 kciremhsuK sirhC
kushmer@bnlux1.bnl.gov    <===Try this one first
kushmerick@pofvax.sunysb.edu 

amead@s.psych.uiuc.edu (alan mead) (04/23/91)

kushmer@bnlux1.bnl.gov (christopher kushmerick) writes:

>In article <1991Apr21.211234.17738@ux1.cso.uiuc.edu> amead@s.psych.uiuc.edu (alan mead) writes:
>>*contiguous* chunks (I think--I'm not sure if it's always an error to
>How is one supposed to allocate contiguous blocks?

The manual states (the 5.5 manual under GetMem) that when the heap is
not fragmented, GetMem returns contiguous data (why shouldn't it?).

As to a general solution, yes--I'm not sure it exists.

>Plus I doubt that this would work anyway, because the limitation is the
>size of a segment, minus a few bytes used by the heap manager. Thus
>you could not allocate contiguous blocks, because the blocks would be 
>separated by these housekeeping bytes.

An excellant point, but then why would the manual bother describing the
returned memory as contiguous?  (I mean it *specifically* says something
like "if you want more than 64K...").

Just wondering.

-alan

dmurdoch@watmath.waterloo.edu (Duncan Murdoch) (04/27/91)

There were a couple of misconceptions in earlier postings about trying to
allocate contiguous blocks of size greater than 64K.

1.  Why a limit of 65521 on Getmem allocations?

This comes from the facts that, in versions 4.0 to 5.5, Getmem allocates
the block at the very first place that it can find it, and all routines
that use allocated blocks do 16 bit addressing only.  

If Getmem happens to allocate something at location 1234:000F, then the
65521st byte will be at address 1234:FFFF, and the 65522nd would wrap around
to 1234:0000.

In versions 4.0 to 5.5, the offset portion of an allocated address is
always in the range 0000 to 000F, hence the limit.

In version 6.0, the offset portion is always either 0000 or 0008, so 
presumably the limit is a bit bigger, but the manual doesn't mention it.
Here you'll have to watch out for the fact that you might skip some bytes
at the beginning to achieve the 8 byte granularity, but it should be
a bit easier to get contiguous allocations.

2.  Is a contiguous allocation good enough?

The answer in all versions is no.  Unless you do your own array addressing
in assembler, inline, or pointer arithmetic, there's no way to get at 
anything after the address wraps around.  You can't have an array bigger
than 64K if you want to be able to use it as an array.

Duncan Murdoch
dmurdoch@watstat.waterloo.edu (when it's up :-)