home *** CD-ROM | disk | FTP | other *** search
- 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
- From: bansal@ces.cwru.edu (Tanuj Bansal)
- Newsgroups: comp.lang.fortran
- Subject: Re: Dynamic M-Alloc by f77+C
- Date: 22 Dec 1992 02:26:09 GMT
- Organization: Case Western Reserve University
- Lines: 228
- Distribution: world
- Message-ID: <1h5uc1INNs1h@usenet.INS.CWRU.Edu>
- NNTP-Posting-Host: amethyst.ces.cwru.edu
-
-
- Hi.
-
- I made a similar request, some time back and got a lot of informative
- mail. Finally I mailed a program in this newsgroup, but it's not just
- Fortran, it's a Fortran-c interface...if you are looking for f/c I
- think the sample code I posted is the answer to your original
- poster...
-
- I'm reposting the same, but as I wrote last time, I am not sure how
- mature and *REAL* programmer type solution this is, but yes...it works
- and as per my experience, should be quite portable. I'd appreciate any
- comments or suggestions by the gurus out there.
-
- (If any1 posted some suggestions in this newsgroup, for improving the
- method...I'm sorry, for a long time after that posting, I could not
- follow the activity here ...)
-
-
- First let's try this FoRtRaN-only version: (which'll not work, obv.)
- ~~~~~ (file just-fortran.f)
-
- program mytry_no_c
- m1 = 10
- call boss1(x,m1)
- call boss2(x,m1)
- stop
- end
-
-
-
- subroutine boss1(x, k)
- real x(k)
- do 5 i = 1, k
- x(i) = 5.5
- print *, 'x(',i,') = ', x(i)
- 5 continue
- m3 = 2*k
- return
- end
-
-
-
- subroutine boss2(x, k2)
- real x(k2)
- k3 = k2/2
-
- do 10 i = 1, k3
- print *, 'x(',i,') = ', x(i)
- 10 continue
-
- do 5 i = k3+1, k2
- x(i) = 6.6 + x(i-1)
- print *, 'x(',i,') = ', x(i)
- 5 continue
-
- return
- end
-
- --------------------
- sparc2:unix/my_dir #999> f77 just-fortran.f -o just-fortran
- just-fortran.f:
- MAIN mytry_no_c:
- boss1:
- boss2:
-
- sparc2:unix/my_dir #998> just-fortran
- x( 1) = 5.50000
- x( 2) = 5.50000
- *** Segmentation Violation = signal 11 code 3
- Traceback has been recorded in file:
- /sparc2:unix/my_dir/just-fortran.trace
- Note: Line numbers for system and library calls may be incorrect
- x( 1085276160) = IOT trap
-
-
- sparc2:unix/my_dir #997> cat just-fortran.trace
- Note: Line numbers for system and library calls may be incorrect
- Begin traceback...
- Called from [func: (null)], at 0x1feb2c4c, args=0xb 0x3 0x1ff93a44 0xe00040d8
- Called from [func: (null)], at 0x1ff7be24, args=0xb8 0x1ff9dfb7 0x1ff7a858 0x80
- Called from [func: _boss1_], at 0x2458, args=0x28288 0x6 0x2c2ad84 0x4
- Called from [func: _MAIN_], at 0x22cc, args=0x2ad88 0x2ad8c 0xf7fff738 0x0
- Called from [func: (null)], at 0x1ff40c38, args=0x0 0x2aac4 0x1 0x2e000000
- Called from [func: start], at 0x2064, args=0x0 0x10 0xf7fff81c 0x28000
- End traceback...
-
- ----------------------------
-
- Second: Well....let's try this interface...
- ~~~~~~
-
- ----- Main fortran code (file for-c.f) -----
-
- program mytry_with_c
- m1 = 10
- call do_it_for_me_c(x,m1) ! Going (in) to (get) C(ash)
- stop
- end
-
-
-
- subroutine boss1(x, k)
- real x(k)
- do 5 i = 1, k
- x(i) = 5.5 * float (i) ! *float(i)->To have diff nos.
- print *, 'x(',i,') = ', x(i)
- 5 continue
- m3 = 2*k
- call do_it_for_me_c2(x, m3) ! Going (in) to (get) C(ash)
- return
- end
-
-
-
-
- subroutine boss2(x, k2)
- real x(k2)
- k3 = k2/2
-
- do 10 i = 1, k3
- print *, 'x(',i,') = ', x(i)
- 10 continue
-
- do 5 i = k3+1, k2
- x(i) = 6.6 + x(i-5) ! Check if I lost anything
- print *, 'x(',i,') = ', x(i)
- 5 continue
-
- return
- end
-
-
-
- ----- C code (file bank.c) ------
-
- #include<stdio.h>
-
- do_it_for_me_c_(x,m2)
- float *x;
- int *m2;
- {
- x = (float *) malloc(*m2 * sizeof (float));
- printf("\n\n The initial array is :::: \n\n");
- boss1_(x,m2);
-
- return;
- }
-
- do_it_for_me_c2_(x, m4)
- float *x;
- int *m4;
- {
- x = (float *) realloc(x, *m4 * sizeof(float));
- printf("\n\n The extended array is :::: \n\n");
- boss2_(x,m4);
-
- return;
-
- }
-
-
- ----------------------------------------------------------------
- sparc2:unix/my_dir #996> cc -c bank.c
- sparc2:unix/my_dir #995> f77 for-c.f bank.o -o for-c
- for-c.f:
- MAIN mytry_with_c:
- boss1:
- boss2:
- sparc2:unix/my_dir #994>for-c
-
-
- The initial array is ::::
-
- x( 1) = 5.50000
- x( 2) = 11.0000
- x( 3) = 16.5000
- x( 4) = 22.0000
- x( 5) = 27.5000
- x( 6) = 33.0000
- x( 7) = 38.5000
- x( 8) = 44.0000
- x( 9) = 49.5000
- x( 10) = 55.0000
-
-
- The extended array is ::::
-
- x( 1) = 5.50000
- x( 2) = 11.0000
- x( 3) = 16.5000
- x( 4) = 22.0000
- x( 5) = 27.5000
- x( 6) = 33.0000
- x( 7) = 38.5000
- x( 8) = 44.0000
- x( 9) = 49.5000
- x( 10) = 55.0000
- x( 11) = 39.6000
- x( 12) = 45.1000
- x( 13) = 50.6000
- x( 14) = 56.1000
- x( 15) = 61.6000
- x( 16) = 46.2000
- x( 17) = 51.7000
- x( 18) = 57.2000
- x( 19) = 62.7000
- x( 20) = 68.2000
-
-
- ---------------------------------------------------
-
- Note that C code not only allocated the space for an array which can
- be used in the fortran code, it also extended it, preserving the older
- nos, which, in my case, is a prerequisite if I want to use this method.
-
- Tanuj...
-
- ====================================================================
- Tanuj Bansal,
- MS,Deptt. of Civil Engineering,
- MS,Deptt. of Computer Engineering and Science
- Case Western Reserve University, Cleveland, OH.
-
- ({[ My decision about the up/down-case letters is my own and it has not
- been influenced, in any way, by my editor...]})
- ====================================================================
-
-