Uno
7/25/2011 11:46:00 PM
I'm doing a mixed-language exercise, and C is the middle east as far as
that goes. So my listing is going to have non-standard parts, I'll put
them at the top, so that people who chaff at the site of another syntax
won't have to retreat to under their bridges.
It starts with fortran the common C extension:
$ pwd
/home/dan/source/fortran_stuff
$ gcc -c -Wall -Wextra cfunc2.c -o cfunc.o
$ gfortran -c -Wall -Wextra caller2.f03 -o caller.o
$ gfortran -Wall -Wextra caller.o testc.o -o out
$ ./out
this sentence is less than fifty bytes.
Those were the compilation commands and output.
$ cat caller2.f03
program test
use iso_c_binding
implicit none
interface
function testc() bind(c) result(pa)
use iso_c_binding
type(c_ptr):: pa
end function testc
end interface
type(c_ptr), target :: pa
character(1, c_char),pointer::fpa(:)
pa = testc()
call c_f_pointer(pa, fpa, [50])
print*, fpa(1:50)
end program test
! gfortran -c -Wall -Wextra caller2.f03 -o caller.o
! gfortran -Wall -Wextra caller.o testc.o -o out
$
Fortran pointers and C pointers aren't the same thing at all. With the
c part I compiled with non-standard headers, but I didn't need them, so
I'll re-order them, and then everything below that purports to be
standard C:
$ cat cfunc2.c
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define RIGHT HERE
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
char * testc() {
char * pa;
pa = (char *)malloc (50);
if (pa == NULL)
{
printf ("malloc failed.\n");
exit (EXIT_FAILURE);
}
strcpy((char *)pa, "this sentence is less than fifty bytes.");
return pa;
}
// gcc -c -Wall -Wextra cfunc2.c -o cfunc.o
I tried to follow Heathfield's development in chp 8 of _unleashed_ for
memory management. For obvious reason, I don't free what I've
malloc'ed, because then it would suck as a pass.
Any comments about the code south of RIGHT HERE are welcome. Cheers,
--
Uno