Path: utzoo!attcan!uunet!lll-winken!lll-tis!helios.ee.lbl.gov!pasteur!agate!eos!amelia!lemming.nas.nasa.gov!fouts From: fouts@lemming.nas.nasa.gov.nas.nasa.gov (Marty Fouts) Newsgroups: comp.lang.fortran Subject: Re: Fortran versus C for numerical analysis Message-ID: <984@amelia.nas.nasa.gov> Date: 14 Sep 88 23:28:56 GMT References: <1484@ficc.uu.net> <3502@lanl.gov> Sender: news@amelia.nas.nasa.gov Reply-To: fouts@lemming.nas.nasa.gov.nas.nasa.gov (Marty Fouts) Organization: NASA Ames Research Center Lines: 98 In article <3502@lanl.gov> jlg@lanl.gov (Jim Giles) writes: > >No, I've never seen a C dynamically allocated multidimensional array >that wasn't implemented as an array of pointers. For a two dimensional >array, each pointer in the array of pointers points to a row of the array. >Higher dimensions are implementes as pointers_to_pointers_to_.... >the number of indirect levels is one less than the number of dimensions. > >- This is _NOT_ 'like any other array'. Jim, here's one: main() { float x[10][10]; fprintf(stdout,"Hi Jim!\n"); exit(0); } Most C compilers will generate the same kind of 2d array code for this as any other language with adjacently spaced arrays. The array qualifies as dynamic, since it is local/automatic and gets allocated on the stack when the procedure is run. (;-) I think various people are quibbling over poor definitions at this point. Fortran doesn't support dynamic arrays -- in the sense of space allocated at runtime -- at all. (Well, it didn't before the "SAVE" statement introduced the possiblility of recursion. Now it does.) C supports two different kinds of dynamic arrays: Call stack allocated and memory allocator returned. Fortran doesn't support memory allocator returned. (No pointers) The above is an example of call stack allocated. memory allocator returned is slightly different. Fortran does support conformant arrays (sort of) in subroutine references This SUBROUTINE A(X,N,M) DIMENSION X(N,M) is legal, but this SUBROUTINE A(X,N,M) DIMENSION X(N,M), Y(N,M) is not. This has been the cause of considerable grief to Fortran programers over the years. C only sort of has conformant arrays. The C declaration suba(x) real x[][N]; is a sort of conformant C array. C lets you get away with one dimension being unspecified, but requires you specify the others. Conformant local arrays are also quite illegal in C. It is true that C compilers can't generate efficent code in the general case of memory allocated dynamic arrays, but Fortran compilers can't generate any code at all. (;-) Further, C compilers can, and some do, generate good code when they detect explicit dope vectors. In order to avoid the math of index calculation, many compilers generate offset index vectors. There is a C idiom (See Numerical Recipes in C's appendices for an example) which looks exactly like a dope vector. At least one compiler I am aware of can sometimes detect and always be told that a dynamic array is of this form, allowing the compiler to generate exactly the code you would expect for a contiguously allocated array. Neither language has all of the features that I could use for arrays in numerical work. A partial list (off the top of my head) includes: o Range specification, ala Pascal's [n..m] format. o The ability to query an array for its shape. By shape, I mean the number of indices and the ranges of each index. o not having to specify the shape of an array when it is passed as an argument. o True dynamic (all senses) multiple dimensioned arrays in subroutines. o good checking for uninitialized reference errors and index out of bound errors which I can selectively enable at runtime. o reduction operators (;-) With respect to array semantics, Fortran is very good in the presences of vector machines and naive compiler technology. C is very good for dynamically varying requirements in memory tight environments. I can easily make C behave like Fortran for many applications; I have a great deal of trouble going the other way. Marty +-+-+-+ I don't know who I am, why should you? +-+-+-+ | fouts@lemming.nas.nasa.gov | | ...!ames!orville!fouts | | Never attribute to malice what can be | +-+-+-+ explained by incompetence. +-+-+-+