Path: utzoo!attcan!uunet!cs.utexas.edu!tut.cis.ohio-state.edu!ukma!gatech!prism!loligo!pepke From: pepke@loligo.cc.fsu.edu (Eric Pepke) Newsgroups: comp.sys.mac.programmer Subject: Re: THINK C Array Design Flaw Message-ID: <723@loligo.cc.fsu.edu> Date: 16 May 89 21:18:04 GMT References: <664@loligo.cc.fsu.edu> <24093@agate.BERKELEY.EDU> <696@loligo.cc.fsu.edu> <698@loligo.cc.fsu.edu> <2595@helios.ee.lbl.gov> Reply-To: pepke@loligo.UUCP (Eric Pepke) Organization: Supercomputer Computations Research Institute Lines: 52 In article <2595@helios.ee.lbl.gov> beard@ux3.lbl.gov (Patrick C Beard) writes: >I'm sorry, but I just couldn't let this discussion go by without making >a comment. The problem you are discussing, passing static or automatic arrays, >such as foo[40][50] to subroutines with a declaration such as foo[][50] is >doomed to fail not because of a failing in THINK C (formerly LightSpeed C), >but because of an inherent flaw (feature?) in the C programming language. >Namely, the duality between arrays and pointers makes passing arrays of >dimension >= 2 impossible. The reason is that the compiler assumes that >a declaration of foo[][50] really means *foo[50], an array of pointers. >The array foo[40][50] is really just one long chunk of memory, and an >index into it, such as foo[y][x] is really just foo+40*y+x. The called >subroutine has no knowledge of the inner dimensions of the array, and so. >has to resort to foo[y][x] meaning *(*(foo+y)+x), or *(foo[y]+x). Not true. I just tried compiling ArrayRef(foo, a, b, c) int foo[][50][50]; int a, b, c; { register int x; x = foo[a][b][c]; } PointerRef(foo, a, b, c) int *foo[50][50]; int a, b, c; { register int x; x = foo[a][b][c]; } in Lightspeed/THINK C. They generate different code. The code is correct in each case. ArrayRef indexes one long 3-D array, and PointerRef gets a pointer to some integers from a 2-D array of pointers and then adds to the pointer to get the address. So, it although the function bodies look the same, the compiler does the intuitively correct thing based on the declaration. By the way, I have gotten the application working using Lightspeed C, and it does include a big 3-D array which works fine. Thank you once again, Rich and company, for a wonderful development system. Had I not realized my my mistake and switched from MPW C I would probably still be waiting for it to compile. This was the first time I have used the Palette manager, and the speed of your C made exploring its behavior a lot less painful. Eric Pepke ARPA: pepke@gw.scri.fsu.edu Supercomputer Computations Research Institute MFENET: pepke@fsu Florida State University SPAN: pepke@scri Tallahassee, FL 32306-4052 BITNET: pepke@fsu Disclaimer: My employers seldom even LISTEN to my opinions. Meta-disclaimer: Any society that needs disclaimers has too many lawyers.