[comp.lang.pascal] implementing large arrays in turbo pascal

CRAIG_F_OCHIKUBO@cup.portal.com (10/16/87)

I need some help in writing a turbo pascal program.  What I am
trying to do is to write an image processing program that
will be able to do image compression and enhancement.  The
problem i run across is the memorylimit in turbo, and in
having a clean way of implementing a large matrix in turbo.

Any assistance would be greatly appreciated!!

Thanks,
Craig Ochikubo

16448591%VUVAXCOM.BITNET@wiscvm.wisc.EDU (10/17/87)

Subject: implementing large arrays in turbo pascal


in <973@cup.portal.com> you write:
>I need some help in writing a turbo pascal program.  What I am
>trying to do is to write an image processing program that
>will be able to do image compression and enhancement.  The
>problem i run across is the memorylimit in turbo, and in
>having a clean way of implementing a large matrix in turbo.
>

The only way I know to get around turbo pascal's memory limit with large
arrays is to create an array that is really a structure referenced through
a pointer.  For example, suppose the following types are defined in a program:

array_type = array[0..45000] of real;
array_ptr  = ^array_type;

These type statements set up the type for the array and a pointer to it.

Now, let's suppose that the following is part of a var statement:

var array1, array2 : array_ptr;

This sets up pointers for array1 and array2 for the large arrays.  To use
them, all that is needed is to create the arrays pointed to by the pointers
array1 and array2.  This is done using the new statment such as:

new(array1);

Remember, when you actually access the arrays, this is done through a pointer,
so any reference to the arrays would be something like:

array2^[200] := 2.0;

There is a *big* problem with this approach to making the large arrays is
that you have to know prior to creating the arrays if there is enough memory
available for them, otherwise all sorts of things can happen (usually the
program just dies, but evil things can happen, too).  There is also an
advantage to this approach since the arrays are referenced through pointers,
you can always dispose of them when you are done and free up memory for
whatever else.



==============================================================================
| Mark Schaffer        | BITNET: 164485913@vuvaxcom                          |
| Villanova University | UUCP:   ...{ihnp4!psuvax1,burdvax,cbmvax,pyrnj,bpa} |
| (Go Wildcats!)       |           !vu-vlsi!excalibur!164485913              |
==============================================================================

ayac071@ut-ngp.UUCP (William T. Douglass) (10/18/87)

In article <9834@brl-adm.ARPA> 16448591%VUVAXCOM.BITNET@wiscvm.wisc.EDU writes:
>Subject: implementing large arrays in turbo pascal
>
>
>in <973@cup.portal.com> you write:
>>I need some help in writing a turbo pascal program.  What I am
>>trying to do is to write an image processing program that
>>will be able to do image compression and enhancement.  The
>>problem i run across is the memorylimit in turbo, and in
>>having a clean way of implementing a large matrix in turbo.
>>
>
>The only way I know to get around turbo pascal's memory limit with large
>arrays is to create an array that is really a structure referenced through
>a pointer.  For example, suppose the following types are defined in a program:
>
>array_type = array[0..45000] of real;
>array_ptr  = ^array_type;
>
>These type statements set up the type for the array and a pointer to it.
>
Sorry, Mark, but this won't work.  1st, TP only supports constants up to
the size MAXINT, which is 32,767 (signed 16-bit quantity, called INTEGER in
TP.)

Secondly, an array of that many reals cannot fit in a single 8086 segment of
64K.  This is a constraint on all data structures running on IBM-PC type
machines.

I recommend creating an array of pointers to arrays, with each individual
array under 64K bytes in size.  One way to do this is:

	type bigarray = array[0..1] of real;  (* or whatever type is needed *)

	var  a1,a2: array[0..1000] of ^bigarray;

begin

	getmem(a1,NUM_OF_BYTES_NEEDED);
        getmem(a2,NUM_OF_BYTES_NEEDED);

	a1[1]^[1] := value1;   (* 1st element of 1st array *)
        a1[1]^[2] := value2;   (* 2nd   "     "   "    "   *)
        a1[2]^[1] := value3;   (* 1st element of 2nd array *)

You're matrix is not stored contiguously in memory, but should function as
a regular matrix for most purposes.

Caveats:
	You must use pointer indirection to access to 2nd array index
	i.e. a1[n]^[m]

	You have to calculate the size needed in bytes when using getmem.  You
	could say      GETMEM(A1,SIZEOF(REAL)*NUM_OF_BYTES_NEEDED);

	You cannot do i/o directly on dynamically-allocated data structures.
	You have to input your values, then assign them to the appropriate
	element.

	Each "ROW" can only be 64K-bytes long.

	You need enough memory to allocate the arrays, lest you get 0-value
	pointers and start storing data on top of the interrupt vectors.  Best
	to use the TP function MEMAVAIL to check of the amount of heap memory
	available before doing the getmem() call.

If you want to discuss this further, drop me a line.

Bill Douglass
ayac071@ngp.UUCP