I allocated value to status array like this :
status[i] += 1;
and I like to access to this array from fortran
how can I access to this array?
for example I want to change the value of STAT from fortran like this :
STAT(2)=3
is this possible?
c source
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/stat.h>
void call_fc_ (int *key, int *addr, int *size, int *status)
{
int i;
int shmid;
void* shared_addr;
//printf("first ptr = %p\n", *addr);
shmid = shmget (*key, *size, IPC_CREAT | IPC_EXCL | 0666);
if (shmid == -1)
{
printf("shmget is failed!\n");
exit(0);
}
shared_addr = (void*) shmat(shmid, 0, 0);
status = (int*)shared_addr;
//printf("status ptr = %p\n", status);
int data_size = *size/sizeof(int);
for(i=0; i<data_size;i++) {
status[i] += 1;
printf("%d th value : %d \n", i, status[i]);
}
}
fortran source
IMPLICIT NONE
INTEGER*8 KEY,SIZE,ADDR
DATA KEY / 777 /
DATA SIZE / 64 /
!DATA ADDR / Z'b76fb000' /
CALL CALL_FC(KEY, ADDR, SIZE, STAT)
PRINT *, 'stat is : ', STAT
! CAN I ACCESS TO STAT LIKE THIS?
!DO I=1,10
!STAT(I) = STAT(I) + 5
!WRITE (*,*) STAT(I)
!END DO
I have been tested this code and I refered to good answers to this question. but I got an segmentation fault error when I tried to do like this :
integer(c_int) :: key = 777, ssize = 64, addr
integer, pointer, dimension(:) :: stat
type(c_ptr) :: statptr
!DATA KEY / 777 /
!DATA SIZE / 64 /
print *, 'before stat size = ', size(stat)
call call_fc(key, addr, ssize, statptr)
!print *, 'statptr = ', statptr
call c_f_pointer(statptr, stat, [ssize])
print *, 'after stat size = ', size(stat)
stat(1) = 111 <==
stat(2) = 222
stat(3) = 333
print *, 'stat : ', stat
can you recognize what the matter is?