I am mixing c and fortran (with c calling fortran) and can’t get an array passed into fortran properly. I use malloc to create an array in c and pass it into fortran. In the fortran, the array is declared with some shape, but when I print the shape, I get garbage. The code is below. Note that this works with no problem using the g95 and intel compilers. Note that this is running on caos linux on opterons. “pgf95 -V” gives me “pgf95 6.1-6 64-bit target on x86-64 Linux”. What is wrong here?
This is the c file, badc.c
#include <stdio.h>
#include <stdlib.h>
void extern setaa_(double *aa);
int main() {
double *aa;
aa = (double *)malloc(6*6*6*sizeof(double));
setaa_(aa);
}
This is the fortran, badfortran.F90
SUBROUTINE setaa(p)
real(kind=8)::p(0:5,0:5,0:5,1,0:0)
print*,"setaa shape p ",shape(p)
return
end subroutine setaa
and here is the makefile
all: bad
badfortran.o: badfortran.F90
pgf95 -r8 -i8 -g -c badfortran.F90
badc.o: badc.c
pgcc -c badc.c
bad: badfortran.o badc.o
pgf95 -Mnomain -o bad badfortran.o badc.o -lm
clean:
rm -f *.o *.mod bad
When I run it, I get output that looks like
setaa shape p 25769803782 4294967302
1 46912499294866 0