🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.atari.st
Article #33101

Re: MWC & large arrays -- help!

#33101
From: braner
Date: Sun, 03 Jul 1988 12:48
41 lines
2088 bytes
In article <542@philmds.UUCP> leo@philmds.UUCP (Leo de Wit) writes:
>
>Just specify the correct pointer type, for instance if you want an
>array of this type:
>
>    unsigned int array[200][500];
>
>then your pointer should be declared:
>
>    int (*arrp)[500]; /* that's just 1 pointer, for pointing to an int [500];*/
>
>you initiate it as follows:
>
>    arrp = (int (*)[500])Malloc(200 * sizeof(*arrp)); /* isn't that nice 8-) */

- err, s.b. Malloc((long)200*500*sizeof(int));
As written above it allocates space for 200 pointers, not arrays.

BUT, a better method is to Malloc or malloc or simply statically allocate
an array of 200 pointers, then allocate (in a for() loop) 200 1-D arrays
of 500 ints, and initialize the pointers to the 1-D arrays.  Now the
pointers can be used as matrix rows:

>and now you can say for instance:
>
>    arrp[155][430] = 12345;
>
>In fact you can use all normal pointer conversions, such as sizeof, *,
>&, [], 'arrp' being a pointer of the same type as 'array'.

Note that for this method you DO write:
>
>    int *arrp[200];	/* not 500 */

With this technique you can get around malloc's 64K limit with portable code,
and matrix access is FASTER, too!  Also, both dimensions of the matrix
can be decided upon at run time (e.g., can be given by parameters to
a function).  In the first method, and in statically allocated arrays,
the dimensions are fixed at compile time, a deficiency of C.

- Moshe Braner
Technical Details
Message-ID: <5366@batcomputer.tn.cornell.edu>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!husc6!bbn!rochester!cornell!batcomputer!braner
References: <734@cacilj.UUCP> <46700008@hcx2> <411@brambo.UUCP> <767@lakesys.UUCP> <2921@tekig5.TEK.COM> <2073@alliant.Alliant.COM> <542@philmds.UUCP>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!husc6!bbn!rochester!cornell!batcomputer!braner
From: braner@batcomputer.tn.cornell.edu (braner)
Newsgroups: comp.sys.atari.st
Subject: Re: MWC & large arrays -- help!
Summary: correction and further suggestion
Message-ID: <5366@batcomputer.tn.cornell.edu>
Date: 3 Jul 88 12:48:01 GMT
References: <734@cacilj.UUCP> <46700008@hcx2> <411@brambo.UUCP> <767@lakesys.UUCP> <2921@tekig5.TEK.COM> <2073@alliant.Alliant.COM> <542@philmds.UUCP>
Reply-To: braner@tcgould.tn.cornell.edu (braner)
Organization: Cornell Theory Center, Cornell University, Ithaca NY
Lines: 41