0

------ main.c---------

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <string.h>

int main()
{   
    char* lib_name = "./a.out";
    int array[5] = {1,2,3,4,5};
    int size_a = sizeof(array)/sizeof(int);            
    void* handle = dlopen(lib_name, RTLD_NOW);
    if (handle) {
        printf("[%s] dlopen(\"%s\", RTLD_NOW): incarcare finalizata\n", 
           __FILE__, lib_name);
    }
    else {
        printf("[%s] nu poate fi deschis: %s\n", __FILE__, dlerror());
        exit(EXIT_FAILURE);
    }
    void (*subrutine_fortran)(int*, int*) = dlsym(handle, "putere");
    if (subrutine_fortran) {
        printf("[%s] dlsym(handle, \"_set_name\"): simbol gasit\n", __FILE__);
    }
    else {
        printf("[%s] simbol negasit: %s\n", __FILE__, dlerror());
        exit(EXIT_FAILURE);
    }



    subrutine_fortran(&array,&size_a);
    //dlclose(handle);
    for(int i=1;i<4;i++) {
    array[i]=array[i]+1;
    }
}

------ hello.f90 --------

subroutine putere(a,h) bind(c)
    use ISO_C_BINDING
    implicit none
    integer(c_int) :: h
    integer(c_int), dimension(h) :: a
    integer i
    do concurrent (i=0:5)
        a(i)=a(i)*10
    end do
    !write (*,*) a
end subroutine

配列要素をループすると、次のようになります。

for(int i=1;i<4;i++) {
  array[i]=array[i]+1;
}

セグメンテーション違反が発生します。

私が書いたときは起こりません:

array[3]=array[3]+1
4

1 に答える 1

2

あなたのCコードはこれです:

int array[5] = {1,2,3,4,5};
int size_a = sizeof(array)/sizeof(int);            

subrutine_fortran(&array,&size_a);

Fortran コードは次のとおりです。

subroutine putere(a,h) bind(c)
    use ISO_C_BINDING
    implicit none
    integer(c_int) :: h
    integer(c_int), dimension(h) :: a
    integer i
    do concurrent (i=0:5)
        a(i)=a(i)*10
    end do
    !write (*,*) a
end subroutine

これはいくつかの点で間違っています。Zack が指摘しているように、Fortran 配列は 1-index です (C などの別の場所からのものであっても)。したがって、これは 1 から開始する必要があります。また、0 が正しい場合、サイズは間違っています。あなたは次のようなものが欲しい

    do concurrent (i=1:h)

その変更により、それは私にとってうまくいきます。

于 2011-08-15T16:46:53.550 に答える