mixing fortran and c with arrays

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

I’ll add further comments to my own post.

The problem seems to have to do with the compiler option -i8. It is doing funny things to the output of the shape command. If I don’t use the -i8 option, it seems to work OK.
Dave

Hi Dave,

It looks like a bug with the SHAPE intrinsic when “-i8” is used. As you noted, it works without “-i8”. Also, it can be simplified even further to:

% cat setaa.f90
program setaa
  real(kind=8)::p(0:5,0:5,0:5,1,0:0)
  print*,"setaa shape p ", shape(p)
  return
end program setaa
% pgf90 setaa.f90
% a.out
 setaa shape p             6            6            6            1
            1
% pgf90 setaa.f90 -i8
% a.out
 setaa shape p               25769803782               4294967302
                        1                        0                  4344992

I’ve added a technical problem report (TRP#3889) and have listed you as the contact. Thank you for reporting this.

  • Mat

Mat,
Thanks - I found the same simple example. In the meantime, I’m going through my code getting rid of the -i8 option since there are some essential places deep in the code that depend on using shape. I just have to make sure the integer and logical constants have there kind specified.
Dave