home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!spool.mu.edu!uwm.edu!rpi!deweeset
- From: deweeset@ptolemy3.rdrc.rpi.edu (Thomas E. DeWeese)
- Newsgroups: comp.sys.mac.programmer
- Subject: Re: Think C 32K segment
- Message-ID: <90p2z+f@rpi.edu>
- Date: 19 Dec 92 20:15:23 GMT
- References: <1go3ohINN4pq@network.ucsd.edu> <chuck-181292154535@choffman.gte.com>
- Distribution: usa
- Organization: Rensselaer Polytechnic Institute, Troy, NY
- Lines: 87
- Nntp-Posting-Host: ptolemy3.rdrc.rpi.edu
-
- In article <chuck-181292154535@choffman.gte.com> chuck@gte.com (Chuck Hoffman) writes:
- >In article <1go3ohINN4pq@network.ucsd.edu>, sah@coast.ucsd.edu (Scott A.
- >Herndon) wrote:
-
- He Want's
- >> int Height[129][129];
-
- >I would try an approach using NewHandle, to get the memory you need at
- >execution time, instead of at compile time. Then initiailize the array
- >after you get it.
-
- OK I agree with you so far. Continue
-
- >typedef struct heightStruct
- > { int h1;
- > int h2;
- > } **heightStructH;
- WHAT are you doing defining a structure?
-
- >myHeightStructH =
- > (heightStructH) NewHandle(129 * sizeof(struct heightStruct));
-
- well this is equvalent to:
- struct heightStruct arry[129]; //Not what he wanted int [129][129]
-
- >/* later, to get info from element 5 ("j") of the structure: */
- >j = 5;
- >workH2 = (*(*myHeightStructH + j)).h2;
- Well a much more intuitive way would be:
- workH2 = (*myHeightStructH)[j].h2; //Although I still don't know why
- //you have a structure defined.
-
- >/* remember about locking/unlocking, and, when done, disposing */
-
- Well there are two "good" ways of doing this. The transparent way and
- the easy way.
- for the transparent way what you want to do is:
-
- int **arry;
- void main(void)
- {
- int i,x;
-
- arry = NewPtr(sizeof(int *)*129);
- for (i=0;i<130;i++)
- arry[i] = NewPtr(sizeof(int)*129);
-
- //Now since we used Ptr's you don't have to worry about locking unlocking.
- //and a reference would be as follows.
-
- x = arry[15][25];
- }
-
- The Good Way(TM) to do this would be
-
- int **arry;
-
- void main(void)
- {
- int i;
-
- arry = NewHandle(sizeof(int *)*129*129); //now everything is one block
-
- //an access would be done as follows
-
- x = (*arry)[15*129+25]; //we need the 15*130 to offset the rows
- //Note that if we were to pass any addresses
- // you would have to lock the memory down
- }
-
- a macro might make this nicer such as
-
- #define index(a,x,y) (*a)[x*129+y]
- ^^^ note hardcoded array row size!
-
- so an access would be:
- x = index(arry,15,25);
-
- BTW you are now using a language that starts arrays at 0. This makes
- indexing, and generating offsets so much nicer. I know that Fortran always
- starts at 1 (and you have to create global arrays, no dynamic mem alloc
- until recently). But one thing you will learn is that while you might think
- it is easier to start indexing at 1, you really are better off getting used
- to indexing at 0. (so your array would by int arry[128][128]) assuming you
- really want and array of 128 elements.
- Thomas DeWeese
- deweeset@rdrc.rpi.edu
-