home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / sys / sgi / 11465 < prev    next >
Encoding:
Text File  |  1992-07-27  |  3.1 KB  |  98 lines

  1. Path: sparky!uunet!elroy.jpl.nasa.gov!sdd.hp.com!caen!uflorida!kluge!serss0!sumargoh
  2. From: sumargoh@serss0.fiu.edu (H. Sumargo)
  3. Newsgroups: comp.sys.sgi
  4. Subject: Calling ForTran Subroutine from C with Dynamic Array
  5. Keywords: Programming
  6. Message-ID: <1776@kluge.fiu.edu>
  7. Date: 27 Jul 92 18:46:52 GMT
  8. Sender: news@kluge.fiu.edu
  9. Reply-To: sumargoh@fiu.edu
  10. Organization: FIU Electrical & Computer Engineering
  11. Lines: 85
  12.  
  13.  
  14. A week ago, I posted some questions regarding how to link ForTran objet code with
  15. C object code.  I got some responses from some of you with some suggestions and
  16. recommendation.  Thank to all of you who responded with help.  Now, I am able to 
  17. link a ForTran object code with a C object code.  However, since I would like my 
  18. program to dynamically allocate the memory for the array,  my program crashed after
  19. calling the ForTran subroutine.  My C main program as well as the ForTran subroutine
  20. are listed as follows:
  21. C main program:
  22.     #include <stdio.h>
  23.     #include <math.h>
  24.     #include <sys/types.h>
  25.     #include <malloc.h>
  26.     double ***z;
  27.     int i, j, k=5, l=3, m, n;
  28.     double ***Allocation (int Frame, int y, int x)
  29.     {
  30.             double ***Mem;
  31.             int i, j;
  32.             if ((Mem = calloc (Frame, sizeof (double *))) == NULL)
  33.             {
  34.                     fprintf (stderr, "Unable to allocate memory\n");
  35.                     exit (1);
  36.             }
  37.             for (j=0; j<Frame; j++)
  38.             {
  39.                     if ((Mem [j] = calloc (Col, sizeof (double *))) == NULL)
  40.                     {
  41.                             fprintf (stderr, "Unable to allocate memory\n");
  42.                             exit (1);
  43.                     }
  44.                     for (i=0; i<Col; i++) 
  45.                     {
  46.                             if ((Mem [j][i] = calloc (Row, sizeof (double))) == NULL)
  47.                             {
  48.                                     fprintf (stderr, "Unable to allocate memory\n");
  49.                                     exit (1);
  50.                             }
  51.                     }
  52.             }
  53.             return (Mem);
  54.     }
  55.  
  56.     main ()
  57.     {
  58.             z = Allocation (2, k, l);
  59.             for (m=0; m<2; m++)
  60.             {
  61.                     n = m + 1;
  62.                     fprintf (stderr, "m=%d  n=%d\n", m, n);
  63.                     sub2_ (z [m], &k, &l, &n);
  64.                     for (j=0; j<k; j++)
  65.                     {
  66.                            for (i=0; i<l; i++)
  67.                                    fprintf(stderr, "%+f\t", z [m][j][i]);
  68.  
  69.                            fprintf (stderr, "\n");
  70.                     }
  71.             }
  72.             free (z);
  73.     }
  74.  
  75. ForTran sub-routine:
  76.         SUBROUTINE SUB2 (A, k1, k2, k3)
  77.         DOUBLE PRECISION A (k2, k1)
  78.         INTEGER i, j, k1, k2, k3
  79.  
  80.         WRITE (6, *) 'In FORTRAN subroutine 2'
  81.         DO j = 1, k2
  82.            DO i = 1, k1
  83.               A (j, i) = i * j * k3 * 1.0
  84.            END DO
  85.         END DO
  86.  
  87.         RETURN
  88.         END
  89. -----------
  90.  
  91. Here is the output:
  92.         m=0     n=1
  93.          In FORTRAN subroutine 2
  94.         Segmentation fault (core dumped)
  95.  
  96. If you have any suggestion or pointers, please email it to sumargoh@fiu.edu.  Thank you
  97. very much in advanced.
  98.