Calling C function from Fortran with pointers

cwheat

I am attempting to create a function in C that is called from Fortran. First things first. The fortran code is being compiled with f77, and the c is compiled with gcc. Both are compiled into .so libraries. The c function is going to read the memory on a device at an address, and size, specified by the fortran. I am able to see the address, and size being passed to the c, but I am having trouble filling the data in the c function and returning it to the fortran. See the relevant code below. My assumption is that there is something wrong in the memory allocation or pointer syntax for the data variable.

C

void copymemory_( uint32_t *addr, int *size, uint8_t *data )
{

    int i;
    printf("addr %x \n", *addr);
    printf("size %i \n", *size);

    for ( i = 0; i<*size; i++)
    {
        *data[i] = i;
        printf("memory %i \n",*data[i]);
    }

}

Fortran

integer memory(4)
call copymemory(z'dead', 4, memory)

DO i = 1,memsize
   call printf(memory(i))
END DO
Vladimir F

I have several points to your code.

Please, supply compilable code and describe the output of that exact code!

That includes the #include for standard headers, if you want people to debug your code, make it easy for them so that they don't have to search which lines did you omit because they seemed "obvious" to you. Just paste everything. The code should compile when copied from here!

Even the executable part of your code does not compile in my compiler. I had to change the *data to data. Are you sure you copied your actual code?

cfun.c: In function ‘copymemory_’:
cfun.c:13:9: error: invalid type argument of unary ‘*’ (have ‘int’)
         *data[i] = i;
         ^
cfun.c:14:31: error: invalid type argument of unary ‘*’ (have ‘int’)
         printf("memory %i \n",*data[i]);
                           ^

Your Fortran code contains a call to some printf subroutine. Where is this defined? Is it present in your actual code? I doubt so. Please copy to StackOverflow always a complete and compilable code.

So after fixing the obvious your present code is:

#include <stdio.h>
#include <stdint.h>

void copymemory_( uint32_t *addr, int *size, uint8_t *data )
{

    int i;
    printf("addr %x \n", *addr);
    printf("size %i \n", *size);

    for ( i = 0; i<*size; i++)
    {
        data[i] = i;
        printf("memory %i \n",data[i]);
    }

}


implicit none

integer :: memsize = 4

integer memory(4)
integer i

call copymemory(z'dead', 4, memory)

DO i = 1,memsize
   print *, (memory(i))
END DO

END

It does not crash but memory in Fortran contains garbage. It has to, because it is integer and you are treating it as int8_t in C.

So either treat it as an array of four integers in C or copy it byte by byte, but then you must pass the correct number of bytes to copy. From your description it is not clear which one is your intention so I will show just one possibility:

void copymemory_( uint32_t *addr, int *size, uint32_t *data )

The output is correct then:

> gfortran-4.10 -fsanitize=address cfun.c ffun.f90 
> ./a.out 
addr dead 
size 4 
memory 0 
memory 1 
memory 2 
memory 3 
           0
           1
           2
           3

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a Fortran function from Qt C++

From Dev

Calling C function with **int parameter from Fortran

From Dev

Calling C function with pointers from F#

From Dev

How to explain calling a C function from an array of function pointers?

From Dev

Pointer is being being masked when calling a C function from Fortran

From Dev

Calling function from Fortran DLL with Char as parameter

From Dev

Calling Member function pointers from vector list

From Dev

Swift: Create Array of Pointers For Calling a C Function

From Dev

Calling C from fortran (ifort, gfortran)

From Dev

Calling a FORTRAN subroutine from C#

From Dev

Calling a C function from Julia and passing a 2D array as a pointer of pointers as argument

From Dev

Calling a Matlab function in Fortran

From Dev

Calling Member Function Pointers

From Dev

Fortran derived types containing pointers to be accessible from C

From Dev

Error Intel Fortran calling C shared library function that receives an integer

From Dev

Calling a function from a map of pointers to functions having a variable number arguments

From Dev

Fortran and C interoperability: receive return value from c (calling from Fortran)

From Dev

Calling FORTRAN subroutine from c++ yields illegal parameter value

From Dev

Calling Fortran subroutines with optional arguments from C++

From Dev

Calling Fortran from C# in VS2019 with iFort

From Dev

calling c function from MATLAB?

From Dev

fortran type missmatch calling function

From Dev

Fortran: Calling other functions in a function

From Dev

Calling a C library method from swift using pointers

From Dev

Java JNI NullPointerException after calling method from C with valid pointers

From Dev

Passing primitive pointers to C function from Cython

From Dev

Return two pointers from a function in c

From Dev

Calling main function from another function in C

From Dev

C Wrapper Calling Fortran Functions

Related Related

  1. 1

    Calling a Fortran function from Qt C++

  2. 2

    Calling C function with **int parameter from Fortran

  3. 3

    Calling C function with pointers from F#

  4. 4

    How to explain calling a C function from an array of function pointers?

  5. 5

    Pointer is being being masked when calling a C function from Fortran

  6. 6

    Calling function from Fortran DLL with Char as parameter

  7. 7

    Calling Member function pointers from vector list

  8. 8

    Swift: Create Array of Pointers For Calling a C Function

  9. 9

    Calling C from fortran (ifort, gfortran)

  10. 10

    Calling a FORTRAN subroutine from C#

  11. 11

    Calling a C function from Julia and passing a 2D array as a pointer of pointers as argument

  12. 12

    Calling a Matlab function in Fortran

  13. 13

    Calling Member Function Pointers

  14. 14

    Fortran derived types containing pointers to be accessible from C

  15. 15

    Error Intel Fortran calling C shared library function that receives an integer

  16. 16

    Calling a function from a map of pointers to functions having a variable number arguments

  17. 17

    Fortran and C interoperability: receive return value from c (calling from Fortran)

  18. 18

    Calling FORTRAN subroutine from c++ yields illegal parameter value

  19. 19

    Calling Fortran subroutines with optional arguments from C++

  20. 20

    Calling Fortran from C# in VS2019 with iFort

  21. 21

    calling c function from MATLAB?

  22. 22

    fortran type missmatch calling function

  23. 23

    Fortran: Calling other functions in a function

  24. 24

    Calling a C library method from swift using pointers

  25. 25

    Java JNI NullPointerException after calling method from C with valid pointers

  26. 26

    Passing primitive pointers to C function from Cython

  27. 27

    Return two pointers from a function in c

  28. 28

    Calling main function from another function in C

  29. 29

    C Wrapper Calling Fortran Functions

HotTag

Archive