home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!elroy.jpl.nasa.gov!sdd.hp.com!caen!uflorida!kluge!serss0!sumargoh
- From: sumargoh@serss0.fiu.edu (H. Sumargo)
- Newsgroups: comp.sys.sgi
- Subject: Calling ForTran Subroutine from C with Dynamic Array
- Keywords: Programming
- Message-ID: <1776@kluge.fiu.edu>
- Date: 27 Jul 92 18:46:52 GMT
- Sender: news@kluge.fiu.edu
- Reply-To: sumargoh@fiu.edu
- Organization: FIU Electrical & Computer Engineering
- Lines: 85
-
-
- A week ago, I posted some questions regarding how to link ForTran objet code with
- C object code. I got some responses from some of you with some suggestions and
- recommendation. Thank to all of you who responded with help. Now, I am able to
- link a ForTran object code with a C object code. However, since I would like my
- program to dynamically allocate the memory for the array, my program crashed after
- calling the ForTran subroutine. My C main program as well as the ForTran subroutine
- are listed as follows:
- C main program:
- #include <stdio.h>
- #include <math.h>
- #include <sys/types.h>
- #include <malloc.h>
- double ***z;
- int i, j, k=5, l=3, m, n;
- double ***Allocation (int Frame, int y, int x)
- {
- double ***Mem;
- int i, j;
- if ((Mem = calloc (Frame, sizeof (double *))) == NULL)
- {
- fprintf (stderr, "Unable to allocate memory\n");
- exit (1);
- }
- for (j=0; j<Frame; j++)
- {
- if ((Mem [j] = calloc (Col, sizeof (double *))) == NULL)
- {
- fprintf (stderr, "Unable to allocate memory\n");
- exit (1);
- }
- for (i=0; i<Col; i++)
- {
- if ((Mem [j][i] = calloc (Row, sizeof (double))) == NULL)
- {
- fprintf (stderr, "Unable to allocate memory\n");
- exit (1);
- }
- }
- }
- return (Mem);
- }
-
- main ()
- {
- z = Allocation (2, k, l);
- for (m=0; m<2; m++)
- {
- n = m + 1;
- fprintf (stderr, "m=%d n=%d\n", m, n);
- sub2_ (z [m], &k, &l, &n);
- for (j=0; j<k; j++)
- {
- for (i=0; i<l; i++)
- fprintf(stderr, "%+f\t", z [m][j][i]);
-
- fprintf (stderr, "\n");
- }
- }
- free (z);
- }
-
- ForTran sub-routine:
- SUBROUTINE SUB2 (A, k1, k2, k3)
- DOUBLE PRECISION A (k2, k1)
- INTEGER i, j, k1, k2, k3
-
- WRITE (6, *) 'In FORTRAN subroutine 2'
- DO j = 1, k2
- DO i = 1, k1
- A (j, i) = i * j * k3 * 1.0
- END DO
- END DO
-
- RETURN
- END
- -----------
-
- Here is the output:
- m=0 n=1
- In FORTRAN subroutine 2
- Segmentation fault (core dumped)
-
- If you have any suggestion or pointers, please email it to sumargoh@fiu.edu. Thank you
- very much in advanced.
-