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
memsizeandprintfand what is thatz'dead'doing where it is? On the C side, whyuint8_t? Why can't you use standard Fortran-C interoperability?