Should I convert FORTRAN code to C?

Richard Harter g-rh at cca.CCA.COM
Mon Jun 20 13:45:52 AEST 1988


In article <738 at naucse.UUCP> rrr at naucse.UUCP (Bob Rose ) writes:
>In article <20340 at beta.lanl.gov>, jlg at beta.lanl.gov (Jim Giles) writes:

>> Awkward isn't the problem.  I don't like the double bracket notation,
>> but anyone can write a macro to turn ARRAY(i,j,k) into array[i][j][k].
>> The Problem is that 2-d arrays are not optimizable because they are
>> immediately turned into pointer-to-pointer-to-data type of constructs.

>Ok, what gives. Some else post the following code (not exact but close enough)
>
>	real a(5,2)
>	...
>	call foo(a)
>	...
>	end
>
>	subroutine foo(a)
>	real a(10)
>	...

	My example, but this isn't the right context.  This code was
posted in response to someone proposing a macro which would produce
the following C code:

	double **a;
	a = (double **)malloc(2 * sizeof(double *));
	for (i=0;i<2;i++) a[i] = (double *)malloc(5 *sizeof(double));

which is not the same thing at all as your example.  However the quoted
statement is not, as far as I know, correct.  Two dimensional arrays in
C are not pointer to array constructs.

>Now doesn't the following c code work to do the same

>	double a[2][5]        /* <-- note [2][5] not [5][2] */
>	...
>	foo(a)
>	...
>	}
>
>	void foo(a)
>	double a[10]
>	{

	Yes, it does.  However in fortran you can do the following

	real a(10)
	...
	call foo(a,2,5)
	call foo(a,5,2)
	...
	subroutine foo(a,m,n)
	real a(m,n)
	...

This is what fortran users mean when they talk about variable dimensioning;
all it really means is that the compiler will generate address calculations
for arrays using variables as well as constants.  You can do this by hand,
but it is much more convenient if the compiler will do it for you.  As far
as I know, there is no good reason why this feature could not be added to
C.  The discussion revolves around ways to fake it in C, using preprocessor
tricks.  They all look kludgy to me.

>Note there are problems if you dynamicly allocate arrays but you cannot
>dynamicly allocate arrays in fortran so this is a void arguement.

	This is not quite right, but it is close enough for practical
purposes.  The correct statements are:

(1)	Does the language provide intrinsic dynamic storage allocation.
Answer:  Intrinsic dynamic storage allocation is not a part of either language.

(2)	Does the standard support environment include a dynamic storage
allocator?  Answer:  C does, fortran doesn't.

(3)	Given a dynamic storage allocator, does the language permit use of
it?	Answer:  The C language provides a convenient portable means for
doing so.  It can be done in fortran, but the means are not as convenient
and cannot be guaranteed to be portable.
-- 

In the fields of Hell where the grass grows high
Are the graves of dreams allowed to die.
	Richard Harter, SMDS  Inc.



More information about the Comp.lang.c mailing list