Path: 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 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