home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / fortran / 4828 < prev    next >
Encoding:
Text File  |  1992-12-22  |  5.7 KB  |  240 lines

  1. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!alpha.ces.cwru.edu!bansal
  2. From: bansal@ces.cwru.edu (Tanuj Bansal)
  3. Newsgroups: comp.lang.fortran
  4. Subject: Re: Dynamic M-Alloc by f77+C
  5. Date: 22 Dec 1992 02:26:09 GMT
  6. Organization: Case Western Reserve University
  7. Lines: 228
  8. Distribution: world
  9. Message-ID: <1h5uc1INNs1h@usenet.INS.CWRU.Edu>
  10. NNTP-Posting-Host: amethyst.ces.cwru.edu
  11.  
  12.  
  13. Hi.
  14.  
  15. I made a similar request, some time back and got a  lot of informative
  16. mail. Finally I mailed a program in this newsgroup,  but it's not just
  17. Fortran, it's a Fortran-c  interface...if you  are  looking for f/c  I
  18. think   the sample code   I posted  is  the  answer  to your  original
  19. poster...
  20.  
  21. I'm reposting the  same, but as I wrote  last time, I  am not sure how
  22. mature and *REAL* programmer type solution this is, but yes...it works
  23. and as per my experience, should be quite portable. I'd appreciate any
  24. comments or suggestions by the gurus out there.
  25.  
  26. (If any1 posted some suggestions  in this newsgroup, for improving  the
  27. method...I'm sorry, for a  long time after that  posting, I could  not
  28. follow the activity here ...)
  29.  
  30.  
  31. First let's try this FoRtRaN-only version: (which'll not work, obv.)
  32. ~~~~~ (file just-fortran.f)
  33.  
  34.       program mytry_no_c
  35.       m1 = 10
  36.       call boss1(x,m1)
  37.       call boss2(x,m1)
  38.       stop
  39.       end
  40.  
  41.  
  42.  
  43.       subroutine boss1(x, k)
  44.       real x(k)
  45.       do 5 i = 1, k
  46.          x(i) =  5.5 
  47.       print *, 'x(',i,') = ', x(i)
  48.  5    continue
  49.       m3 = 2*k
  50.       return
  51.       end
  52.  
  53.  
  54.  
  55.       subroutine boss2(x, k2)
  56.       real x(k2)
  57.       k3 = k2/2
  58.  
  59.       do 10 i = 1, k3
  60.          print *, 'x(',i,') = ', x(i)
  61.  10   continue
  62.  
  63.       do 5 i = k3+1, k2
  64.          x(i) = 6.6 + x(i-1)
  65.          print *, 'x(',i,') = ', x(i)
  66.  5    continue
  67.       
  68.       return
  69.       end
  70.  
  71. --------------------
  72. sparc2:unix/my_dir #999> f77 just-fortran.f -o just-fortran
  73. just-fortran.f:
  74.  MAIN mytry_no_c:
  75.         boss1:
  76.         boss2:
  77.  
  78. sparc2:unix/my_dir #998> just-fortran
  79. x(  1) =     5.50000
  80. x(  2) =     5.50000
  81. *** Segmentation Violation = signal 11 code 3 
  82. Traceback has been recorded in file:
  83.          /sparc2:unix/my_dir/just-fortran.trace 
  84. Note: Line numbers for system and library calls may be incorrect 
  85. x(  1085276160) = IOT trap
  86.  
  87.  
  88. sparc2:unix/my_dir #997> cat just-fortran.trace
  89. Note: Line numbers for system and library calls may be incorrect 
  90. Begin traceback...
  91. Called from [func: (null)], at 0x1feb2c4c, args=0xb 0x3 0x1ff93a44 0xe00040d8
  92. Called from [func: (null)], at 0x1ff7be24, args=0xb8 0x1ff9dfb7 0x1ff7a858 0x80
  93. Called from [func: _boss1_], at 0x2458, args=0x28288 0x6 0x2c2ad84 0x4
  94. Called from [func: _MAIN_], at 0x22cc, args=0x2ad88 0x2ad8c 0xf7fff738 0x0
  95. Called from [func: (null)], at 0x1ff40c38, args=0x0 0x2aac4 0x1 0x2e000000
  96. Called from [func: start], at 0x2064, args=0x0 0x10 0xf7fff81c 0x28000
  97. End traceback...
  98.  
  99. ----------------------------
  100.  
  101. Second: Well....let's try this interface...
  102. ~~~~~~
  103.  
  104. ----- Main fortran code (file for-c.f) -----
  105.  
  106.       program mytry_with_c
  107.       m1 = 10
  108.       call do_it_for_me_c(x,m1)  ! Going (in) to (get) C(ash)
  109.       stop
  110.       end
  111.  
  112.  
  113.  
  114.       subroutine boss1(x, k)
  115.       real x(k)
  116.       do 5 i = 1, k
  117.          x(i) =  5.5 * float (i)   ! *float(i)->To have diff nos.
  118.       print *, 'x(',i,') = ', x(i)
  119.  5    continue
  120.       m3 = 2*k
  121.       call do_it_for_me_c2(x, m3)  ! Going (in) to (get) C(ash)
  122.       return
  123.       end
  124.  
  125.  
  126.  
  127.  
  128.       subroutine boss2(x, k2)
  129.       real x(k2)
  130.       k3 = k2/2
  131.  
  132.       do 10 i = 1, k3
  133.          print *, 'x(',i,') = ', x(i)
  134.  10   continue
  135.  
  136.       do 5 i = k3+1, k2
  137.          x(i) = 6.6 + x(i-5)  ! Check if I lost anything
  138.          print *, 'x(',i,') = ', x(i)
  139.  5    continue
  140.       
  141.       return
  142.       end
  143.  
  144.  
  145.  
  146. ----- C code (file bank.c) ------
  147.  
  148. #include<stdio.h>
  149.  
  150. do_it_for_me_c_(x,m2)
  151.      float *x;
  152.      int *m2;
  153. {
  154.     x = (float *) malloc(*m2 * sizeof (float));
  155.     printf("\n\n The initial array is :::: \n\n");
  156.     boss1_(x,m2);
  157.  
  158.     return;
  159. }
  160.  
  161. do_it_for_me_c2_(x, m4)
  162.      float *x;
  163.      int *m4;
  164. {
  165.     x = (float *) realloc(x, *m4 * sizeof(float));
  166.     printf("\n\n The extended array is :::: \n\n");
  167.     boss2_(x,m4);
  168.     
  169.     return;
  170.     
  171. }
  172.  
  173.  
  174. ----------------------------------------------------------------
  175. sparc2:unix/my_dir #996> cc -c bank.c
  176. sparc2:unix/my_dir #995> f77 for-c.f bank.o -o for-c
  177. for-c.f:
  178.  MAIN mytry_with_c:
  179.         boss1:
  180.         boss2:
  181. sparc2:unix/my_dir #994>for-c
  182.  
  183.  
  184.  The initial array is :::: 
  185.  
  186. x(  1) =     5.50000
  187. x(  2) =     11.0000
  188. x(  3) =     16.5000
  189. x(  4) =     22.0000
  190. x(  5) =     27.5000
  191. x(  6) =     33.0000
  192. x(  7) =     38.5000
  193. x(  8) =     44.0000
  194. x(  9) =     49.5000
  195. x(  10) =     55.0000
  196.  
  197.  
  198.  The extended array is :::: 
  199.  
  200. x(  1) =     5.50000
  201. x(  2) =     11.0000
  202. x(  3) =     16.5000
  203. x(  4) =     22.0000
  204. x(  5) =     27.5000
  205. x(  6) =     33.0000
  206. x(  7) =     38.5000
  207. x(  8) =     44.0000
  208. x(  9) =     49.5000
  209. x(  10) =     55.0000
  210. x(  11) =     39.6000
  211. x(  12) =     45.1000
  212. x(  13) =     50.6000
  213. x(  14) =     56.1000
  214. x(  15) =     61.6000
  215. x(  16) =     46.2000
  216. x(  17) =     51.7000
  217. x(  18) =     57.2000
  218. x(  19) =     62.7000
  219. x(  20) =     68.2000
  220.  
  221.  
  222. ---------------------------------------------------
  223.  
  224. Note that C code not only allocated the  space for an array  which can
  225. be used in the fortran code, it also extended it, preserving the older
  226. nos, which, in my case, is a prerequisite if I want to use this method.
  227.  
  228. Tanuj...
  229.  
  230. ====================================================================
  231. Tanuj Bansal,                           
  232. MS,Deptt. of Civil Engineering, 
  233. MS,Deptt. of Computer Engineering and Science
  234. Case Western Reserve University, Cleveland, OH.
  235.  
  236. ({[ My decision about the up/down-case letters is my own and it has not
  237.     been influenced, in any way, by my editor...]})
  238. ====================================================================
  239.  
  240.