[comp.sys.mac.programmer] Arrays bigger than 32K.

kap1@phyllis.math.binghamton.edu (Dietrich Kappe) (03/30/91)

I'm attempting to compile some code that uses arrays>32K in size.(Using LSC3.0)
The arrays are multi dimensional.

Example: 
	short thing[60][60][8];

Now I know that LSC doesn't allow arrays>32K, so I thought "No problem,
I'll just allocate the memory on the fly."

Attempted Solution:

	typedef short thingType[60][60][8];
	thingType *thing;
	...
	thing=(thingType *)lmalloc(sizeof(thingType));

Of course I still get an illegal array limits error on the typedef statement.
The compiler needs the array bounds for pointer arithmatic, and I certainly
don't want to do all those pointer operations by hand. Any ideas on how I
can get arround the 32K size limit without giving up array dimmension
information?

Thanks in advance,

--Dietrich J. Kappe--
kap1@math.binghamton.edu

hairston@henry.ECE.CMU.EDU (David Hairston) (03/30/91)

[kap1@phyllis.math.binghamton.edu (Dietrich Kappe) writes:]
[] Attempted Solution:
[]
[]	typedef short thingType[60][60][8];
[]	thingType *thing;
[]	...
[]	thing=(thingType *)lmalloc(sizeof(thingType));
[]
[] Of course I still get an illegal array limits error on the typedef
[] statement.  The compiler needs the array bounds for pointer arithmatic,
[] and I certainly don't want to do all those pointer operations by hand.
[] Any ideas on how I can get arround the 32K size limit without giving up
[] array dimmension information?

hmm, this comes up from time to time so i'll post here where it may do
more good.  you could try an approach like this (extreme, but you should
get the point):

the innermost dimension is a pointer to an array of pointers (to an
array of pointers to short integers).  let's call this dimension, dim1
and generate the following typedefs:

typedef	short	***dim1;
typedef	short	**dim2;
typedef	short	*dim3;

or better yet:

typedef	short	*dim3;
typedef	dim3	*dim2;
typedef dim2	*dim1;

hopefully you see that the outer dimension is the only one that really
points to a specific data type (short).  the inner dimensions are merely
pointers to pointers.  now you can get away with something like this:

#define	D1	60
#define	D2	60
#define	D3	8

dim1	thing;
short	i, j, k;

thing = (dim1) malloc(D1 * sizeof(dim2));
/* ... check to see if malloc was okay */

for (i = 0; i < D1; ++i)
{
	thing[i] = (dim2) malloc(D2 * sizeof(dim3));
	/* ... check to see if malloc was okay */
}

for (i = 0; i < D1; ++i)
	for (j = 0; j < D2; ++j)
	{
		thing[i][j] = (dim3) malloc(D3 * sizeof(short));
		/* ... check to see if malloc was okay */
	}

if you're concerned with zero-initialization you can now do:

for (i = 0; i < D1; ++i)
	for (j = 0; j < D2; ++j)
		for (k = 0; k < D3; ++k)
		{
			thing[i][j][k] = 0;
			/* ... or if your'e concerned with pointer math */
			*( *( *( thing + i ) + j ) + k ) = 0;
		}

i said at the outset that this example was extreme.  it is also
non-optimal but i leave it to you to optimize to taste.  one thing
to keep in mind, although "thing" now looks like a three dimensional
array, it isn't!  for example, you can't do ANSI memset()-like
commands on this object.  in all other respects, tho, it behaves
like a three dimensional array.

  -dave-  
hairston@henry.ece.cmu.edu