* Combining FORTRAN and C
@ 1995-11-27 5:00 Leif Laaksonen
1995-11-28 5:00 ` Steve Lionel
1995-11-28 5:00 ` Leif Laaksonen
0 siblings, 2 replies; 3+ messages in thread
From: Leif Laaksonen @ 1995-11-27 5:00 UTC (permalink / raw)
Dear Dec Users,
I have a problem. I used to combine Fortran and c-coding to use the goodies of
both programming languages. For example I use a small c-program to allocate me
the memory dynamically I need in the Fortran main program.
The following piece of code is a small Fortran test program which calls the
c-routine to allocate memory. On the DEC-alpha this code bombs! However,
I manage to run the same code on SGI and IBM boxes. Is this problem related
to the OSF Unix? I can't really understand this? I looked at the address
and it is transfered correctly to the Fortran subroutine but accessing the
memory location bombs the program.
Is there any kind soul who can help me out of this dilemma?
Regards,
-leif laaksonen
-------------------------------------------------------------------
Leif Laaksonen |
Center for Scientific Computing | Phone: 358 0 4572378
P.O. Box 405 | Mobile: 358 400425203
FIN-02101 Espoo | Telefax: 358 0 4572302
FINLAND | Mail: Leif.Laaksonen@csc.fi
---------URL: http://laaksonen.csc.fi/leif.laaksonen.html----------
Tried to save the trees. Bought a plastic bag.
The bottom fell out. It was a piece of crap.
N. Young
-------------------------------------------------------------------
c........................ fortran part.
Program test
implicit double precision (a-h,o-z)
if = 8 * 1000
call fmalloc(iaddress,if)
call subrut(%val(iaddress))
end
subroutine subrut(A)
implicit double precision (a-h,o-z)
dimension A(*)
do i=1,1000,100
a(i)=1.d0*i
end do
call testa(a)
return
end
subroutine testa(A)
implicit double precision (a-h,o-z)
dimension A(*)
do i=1,1000,100
write(6,*) a(i),i
end do
return
end
c...................................................................
/* c-part
interface to a c routine to perform a dynamical
memory allocation
Leif Laaksonen (1992)
Centre for Scientific Computing, Espoo, FINLAND
*/
#include <stdio.h>
#include <sys/types.h>
#include <malloc.h>
/*
bv: is an integer to contain the address to the reserved
memory
reqlen: contains the number of bytes to be reserved
*/
/************************************************************************/
void fmalloc_(bv , reqlen)
int *bv;
int *reqlen;
/************************************************************************/
{
float *BV;
BV = (float *)malloc(*reqlen);
if(BV == NULL) {
perror("Can't allocate memory");
exit(1);}
*bv = (int)BV;
}
/* ................................................................... */
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: Combining FORTRAN and C
1995-11-27 5:00 Combining FORTRAN and C Leif Laaksonen
@ 1995-11-28 5:00 ` Steve Lionel
1995-11-28 5:00 ` Leif Laaksonen
1 sibling, 0 replies; 3+ messages in thread
From: Steve Lionel @ 1995-11-28 5:00 UTC (permalink / raw)
In article <laaksone.33.00C174C1@csc.fi>, laaksone@csc.fi (Leif Laaksonen)
writes:
|>I have a problem. I used to combine Fortran and c-coding to use the goodies of
|>both programming languages. For example I use a small c-program to allocate me
|>the memory dynamically I need in the Fortran main program.
|>
|>The following piece of code is a small Fortran test program which calls the
|>c-routine to allocate memory. On the DEC-alpha this code bombs! However,
|>I manage to run the same code on SGI and IBM boxes. Is this problem related
|>to the OSF Unix? I can't really understand this? I looked at the address
|>and it is transfered correctly to the Fortran subroutine but accessing the
|>memory location bombs the program.
The problem is that Digital UNIX, unlike the other systems, is a 64-bit
address system and your code assumes that addresses fit in 32-bit integers.
I have modified the code as shown below and it works - my C is not the best
so there may be an alternative way of doing it (especially if you can declare
bv as a pointer to a pointer, since C knows what size that is.) Note the
INTEGER*8 declaration in the Fortran code.
There's no need to use C for this purpose - Fortran provides malloc and
other routines in the 3f library. See the DEC Fortran User Manual for
details. DEC Fortran (and probably the others you use) supports the POINTER
statement which is much cleaner to use than the method you have here.
c........................ fortran part.
Program test
implicit double precision (a-h,o-z)
integer*8 iaddress
if = 8 * 1000
call fmalloc(iaddress,if)
call subrut(%val(iaddress))
end
[remainder of Fortran code unchanged]
/* c-part
interface to a c routine to perform a dynamical
memory allocation
Leif Laaksonen (1992)
Centre for Scientific Computing, Espoo, FINLAND
*/
#include <stdio.h>
#include <sys/types.h>
#include <malloc.h>
/*
bv: is an integer to contain the address to the reserved
memory
reqlen: contains the number of bytes to be reserved
*/
/************************************************************************/
void fmalloc_(bv , reqlen)
long *bv;
int *reqlen;
/************************************************************************/
{
float *BV;
BV = (float *)malloc(*reqlen);
if(BV == NULL) {
perror("Can't allocate memory");
exit(1);}
*bv = (long)BV;
}
/* ................................................................... */
--
Steve Lionel Mail: lionel@quark.enet.dec.com
DEC Fortran Development WWW: http://www.digital.com/info/slionel.html
Digital Equipment Corporation CompuServe: 75263,3001
110 Spit Brook Road, ZKO2-3/N30
Nashua, NH 03062-2698 "Free advice is worth every cent"
For information on DEC Fortran 90, see http://www.digital.com/info/hpc/f90/
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: Combining FORTRAN and C
1995-11-27 5:00 Combining FORTRAN and C Leif Laaksonen
1995-11-28 5:00 ` Steve Lionel
@ 1995-11-28 5:00 ` Leif Laaksonen
1 sibling, 0 replies; 3+ messages in thread
From: Leif Laaksonen @ 1995-11-28 5:00 UTC (permalink / raw)
Hello,
I will answer my own question because I know the answer now.
On the DEC osf all addresses are 64 bits and an integer is 32 bits!
My iaddress is an integer => 32 bits. By chaning the iaddress to address
changes the address from 32 bits to 64 and everything works as it should.
Regards,
-leif laaksonen
In article <laaksone.33.00C174C1@csc.fi> laaksone@csc.fi (Leif Laaksonen)
writes:>From: laaksone@csc.fi (Leif Laaksonen)
>Subject: Combining FORTRAN and C
>Date: Mon, 27 Nov 1995 21:51:58 UNDEFINED
>Dear Dec Users,
>I have a problem. I used to combine Fortran and c-coding to use the goodies of
>both programming languages. For example I use a small c-program to allocate me
>the memory dynamically I need in the Fortran main program.
>The following piece of code is a small Fortran test program which calls the
>c-routine to allocate memory. On the DEC-alpha this code bombs! However,
>I manage to run the same code on SGI and IBM boxes. Is this problem related
>to the OSF Unix? I can't really understand this? I looked at the address
>and it is transfered correctly to the Fortran subroutine but accessing the
>memory location bombs the program.
>Is there any kind soul who can help me out of this dilemma?
>Regards,
>-leif laaksonen
>
>-------------------------------------------------------------------
>Leif Laaksonen |
>Center for Scientific Computing | Phone: 358 0 4572378
>P.O. Box 405 | Mobile: 358 400425203
>FIN-02101 Espoo | Telefax: 358 0 4572302
>FINLAND | Mail: Leif.Laaksonen@csc.fi
>---------URL: http://laaksonen.csc.fi/leif.laaksonen.html----------
> Tried to save the trees. Bought a plastic bag.
> The bottom fell out. It was a piece of crap.
> N. Young
>-------------------------------------------------------------------
>c........................ fortran part.
> Program test
> implicit double precision (a-h,o-z)
> if = 8 * 1000
> call fmalloc(iaddress,if)
> call subrut(%val(iaddress))
> end
> subroutine subrut(A)
> implicit double precision (a-h,o-z)
> dimension A(*)
> do i=1,1000,100
> a(i)=1.d0*i
> end do
> call testa(a)
> return
> end
> subroutine testa(A)
> implicit double precision (a-h,o-z)
> dimension A(*)
> do i=1,1000,100
> write(6,*) a(i),i
> end do
> return
> end
>c...................................................................
>/* c-part
> interface to a c routine to perform a dynamical
> memory allocation
> Leif Laaksonen (1992)
> Centre for Scientific Computing, Espoo, FINLAND
>*/
>#include <stdio.h>
>#include <sys/types.h>
>#include <malloc.h>
>/*
> bv: is an integer to contain the address to the reserved
> memory
> reqlen: contains the number of bytes to be reserved
>*/
>/************************************************************************/
>void fmalloc_(bv , reqlen)
> int *bv;
> int *reqlen;
>/************************************************************************/
>{
> float *BV;
> BV = (float *)malloc(*reqlen);
> if(BV == NULL) {
> perror("Can't allocate memory");
> exit(1);}
> *bv = (int)BV;
>}
>/* ................................................................... */
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~1995-11-28 5:00 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1995-11-27 5:00 Combining FORTRAN and C Leif Laaksonen
1995-11-28 5:00 ` Steve Lionel
1995-11-28 5:00 ` Leif Laaksonen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox