Path: utzoo!attcan!uunet!mcvax!philmds!leo From: leo@philmds.UUCP (Leo de Wit) Newsgroups: comp.sys.atari.st Subject: Re: MWC & large arrays -- help! Keywords: Correction of correction, please put on your glasses Message-ID: <549@philmds.UUCP> Date: 4 Jul 88 04:32:29 GMT References: <734@cacilj.UUCP> <46700008@hcx2> <411@brambo.UUCP> <767@lakesys.UUCP> <2921@tekig5.TEK.COM> <2073@alliant.Alliant.COM> <542@philmds.UUCP> <5366@batcomputer.tn.cornell.edu> Reply-To: leo@philmds.UUCP (Leo de Wit) Organization: Philips I&E DTS Eindhoven Lines: 95 In article <5366@batcomputer.tn.cornell.edu> braner@tcgould.tn.cornell.edu (braner) writes: >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. That is NOT correct, you should read better first before critisizing someone else's stuff. It would have been, if it said 'sizeof(arrp)'. But no, it says 'sizeof(*arrp)'; and while (*arrp) is an int[500] - the object which arrp points at - it is correct as it stands. >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: You're changing the subject. The original problem was not to use a different scheme, it said: while you can use Malloc to allocate a large one-dimensional array, it will be difficult for a higher dimension. The tendency of my article was simply that this isn't difficult at all, just use a pointer of the correct type. Yes, I know to how to set up dope vectors, and note that your 'loop' is not so time & space efficient if you allocate the 200 arrays separately. Better allocate it in one chunk (together with a pointer array), then use a for loop to set up the pointers. Furthermore it depends on the situation whether your pointer array is 'better'; might as well use non-constant pointers into the array, this will even prove faster. >>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 */ You misquoted me. I never said that. The original said: then your pointer should be declared: int (*arrp)[500]; /* that's just 1 pointer, for pointing to an int [500];*/ and a bit further: Note that the () are obligatory; int *arrp[500]; are 500 pointers to int, and that's something quite different! What I meant to say was that you had to use () for the pointer declaration I used. Furthermore, while I'm declaring a pointer (that is ONE pointer) to a int [500], the 500 is correct. (and should NOT be 200; it should be 200 in your case). >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. You make it sound as if my code wasn't portable; it IS (at least on every decent compiler). As far as speed is involved, I think using an extra pointer of type int * into the array dynamically is still faster, and uses less space; what if I had as an example: char carr[20000][5]; bit of a waste doing this with pointer arrays, don't you think? As far as the dimensions is conceirned I agree; but this again was not the original problem. Besides, if you want both dimensions free, you have to allocate the pointer array dynamically too. And then all your pointer efficiency is gone (using double indirections). >- Moshe Braner > > Leo.