home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / sys / mac / programm / 18124 < prev    next >
Encoding:
Text File  |  1992-11-08  |  2.0 KB  |  64 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!mcsun!sunic!kth.se!alv.nada.kth.se!d88-jwa
  3. From: d88-jwa@alv.nada.kth.se (Jon WΣtte)
  4. Subject: Re: How to use large arrays in Think C?
  5. Message-ID: <1992Nov7.134925.21715@kth.se>
  6. Sender: usenet@kth.se (Usenet)
  7. Nntp-Posting-Host: alv.nada.kth.se
  8. Organization: Royal Institute of Technology, Stockholm, Sweden
  9. References: <CSTROCKB.92Nov6235644@csws12.cs.sunysb.edu>
  10. Distribution: comp
  11. Date: Sat, 7 Nov 1992 13:49:25 GMT
  12. Lines: 50
  13.  
  14. In <CSTROCKB.92Nov6235644@csws12.cs.sunysb.edu> cstrockb@cs.sunysb.edu (Caleb Strockbine) writes:
  15.  
  16. >if I could use regular array notation for this picture. For instance, if
  17. >I want to just run through every pixel in the image, I'd like to say:
  18.  
  19. >short i, j;
  20.  
  21. >for (i=0;i<256;i++)
  22. >  for (j=0;j<256;j++)
  23. >    ProcessPixel(image[i][j]);
  24.  
  25. >So my question is this: is it possible to allocate a bunch of memory using
  26. >malloc() and then pretend it's a regular array?
  27.  
  28. >I tried a bunch of type casting possibilities, but I couldn't come up
  29. >with anything that worked. Seems like this is something I should know,
  30. >but it's not.
  31.  
  32. Well, I would recommend getting a decent C reference, like
  33. "The C Programming language" by K&R. I have hacked in "C" for
  34. like 5-10 years, and still need to look stuff up sometimes
  35. (like operator precedence, and arrays of functions returning
  36. pointers to functions returning arrays :-)
  37.  
  38. Anyway, you can do this with a typedef. The simple approach
  39. typedef unsigned char largeArray [ 256 ] [ 256 ] ; works
  40. as long as the size is < 32768. It's not here. so you can't
  41. do
  42.     largeArray * foo = malloc ( sizeof ( largeArray ) ) ;
  43. Well, maybe you could, if you turned on "far data."
  44.  
  45. Instead, use a typedef for one row, like:
  46.  
  47. typeDef unsigned char arrayRow [ 256 ] ;
  48.  
  49. and allocate several of them:
  50.  
  51.     arrayRow * foo = malloc ( sizeof ( arrayRow ) * 256 ) ;
  52.  
  53. This will give you the indirection you want:
  54.  
  55.     foo [ 1 ] [ 2 ] = ...
  56.  
  57.                         / h+
  58.  
  59.  
  60. -- 
  61.  -- Jon W{tte, h+@nada.kth.se, Mac Hacker Suedoise (not french speaking) --
  62.  
  63.   "On a clear disc, you can seek forever."
  64.