control the life of a C struct from fortran.

cyril giraudon cyril.giraudon@free.fr
Thu Dec 18 15:46:00 GMT 2008


Hello,

I 'm trying to use libxml2 from gfortran and I don't succeed in created 
the first object :
doc = xmlParseFile(filename)

I 'd like to whether it is possible to create a c_ptr in fortran and tp 
control its life with c function.
The standard is not very clear about c_ptr.

For instance :
##########  C  ###########
typedef struct
{
  int age;
} person

void person_alloc(person* p) { p = malloc(sizeof(person)); return; }
void person_free(person* p) { free(p); return;}
void person_age(person* p) { fprintf(stdout, "age : %d\n", p->age); }

########## Fortran ##########
program
  use iso_c_binding
  ! optional
  type, bind(c) :: person
    integer(kind=c_int) :: age
  end type person
  interface
    subroutine person_alloc(p) bind(c, name="person_alloc")
      type(c_ptr), intent(out) :: p
    end subroutine
    subroutine person_free(p) bind(c, name="person_free")
      type(c_ptr), value :: p
    end subroutine
    subroutine person_age(p) bind(c, name="person_age")
      type(c_ptr), value :: p
    end subroutine
  end interface
  type(c_ptr) :: p
  call person_alloc(p)
  call person_age(p)
  call person_free(p)
end program

All my tests fail :-(

Thanks a lot,

Cyril.





More information about the Fortran mailing list