Quantcast
Channel: Intel® Software - Intel® Visual Fortran Compiler for Windows*
Viewing all articles
Browse latest Browse all 5691

Access to data in COMMON block from C code

$
0
0

Hello!

I faced a problem when trying to create mixed language program - C main program and FORTRAN library. In FORTRAN I made a COMMON block with two allocatable arrays, and I want to have access to both of them from C code. Everything is fine during building and with access to the first one, but while running program can't obtain correct address of the second array (its address is 0x00000004) and breaks with error. Text of both codes is below.

Any suggestion will be highly appreciated - is it possible to correct the code somehow, or I need to create additional subroutine to access data in COMMON block. Of course, I can make several COMMONs for each array, but it also is not convenient.

Thanks for your answer and help.

FORTRAN code*****************************************************

C------------------------------------------------
    SUBROUTINE ALLOC_ARRAYS
    COMMON /MYARRS/ IAR1,IAR2
    INTEGER, DIMENSION(:), POINTER :: IAR1
    INTEGER, DIMENSION(:), POINTER :: IAR2
    INTEGER I
C--------
    ALLOCATE(IAR1(100),IAR2(100))
    DO I = 1,100
        IAR1(I) = I*12
        IAR2(I) = I*10
    END DO
C--------
    END
C------------------------------------------------
    SUBROUTINE DEALLOC_ARRAYS
    COMMON /MYARRS/ IAR1,IAR2
    INTEGER, DIMENSION(:), POINTER :: IAR1
    INTEGER, DIMENSION(:), POINTER :: IAR2
C--------
    DEALLOCATE(IAR1,IAR2)
C--------
    END
C------------------------------------------------

C code*****************************************************

struct for_arrays
{
    int *piAR1;
    int *piAR2;
};

extern "C"
{
    void ALLOC_ARRAYS();
    void DEALLOC_ARRAYS();

    struct for_arrays MYARRS;
}

void main()
{
    ALLOC_ARRAYS();

//    O.k. here
    int it1 = MYARRS.piAR1[5];
//    Error here
    int it2 = MYARRS.piAR2[6];

    DEALLOC_ARRAYS();
}

 


Viewing all articles
Browse latest Browse all 5691

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>