home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.mac.programmer
- Path: sparky!uunet!mcsun!sunic!kth.se!alv.nada.kth.se!d88-jwa
- From: d88-jwa@alv.nada.kth.se (Jon WΣtte)
- Subject: Re: How to use large arrays in Think C?
- Message-ID: <1992Nov7.134925.21715@kth.se>
- Sender: usenet@kth.se (Usenet)
- Nntp-Posting-Host: alv.nada.kth.se
- Organization: Royal Institute of Technology, Stockholm, Sweden
- References: <CSTROCKB.92Nov6235644@csws12.cs.sunysb.edu>
- Distribution: comp
- Date: Sat, 7 Nov 1992 13:49:25 GMT
- Lines: 50
-
- In <CSTROCKB.92Nov6235644@csws12.cs.sunysb.edu> cstrockb@cs.sunysb.edu (Caleb Strockbine) writes:
-
- >if I could use regular array notation for this picture. For instance, if
- >I want to just run through every pixel in the image, I'd like to say:
-
- >short i, j;
-
- >for (i=0;i<256;i++)
- > for (j=0;j<256;j++)
- > ProcessPixel(image[i][j]);
-
- >So my question is this: is it possible to allocate a bunch of memory using
- >malloc() and then pretend it's a regular array?
-
- >I tried a bunch of type casting possibilities, but I couldn't come up
- >with anything that worked. Seems like this is something I should know,
- >but it's not.
-
- Well, I would recommend getting a decent C reference, like
- "The C Programming language" by K&R. I have hacked in "C" for
- like 5-10 years, and still need to look stuff up sometimes
- (like operator precedence, and arrays of functions returning
- pointers to functions returning arrays :-)
-
- Anyway, you can do this with a typedef. The simple approach
- typedef unsigned char largeArray [ 256 ] [ 256 ] ; works
- as long as the size is < 32768. It's not here. so you can't
- do
- largeArray * foo = malloc ( sizeof ( largeArray ) ) ;
- Well, maybe you could, if you turned on "far data."
-
- Instead, use a typedef for one row, like:
-
- typeDef unsigned char arrayRow [ 256 ] ;
-
- and allocate several of them:
-
- arrayRow * foo = malloc ( sizeof ( arrayRow ) * 256 ) ;
-
- This will give you the indirection you want:
-
- foo [ 1 ] [ 2 ] = ...
-
- / h+
-
-
- --
- -- Jon W{tte, h+@nada.kth.se, Mac Hacker Suedoise (not french speaking) --
-
- "On a clear disc, you can seek forever."
-