2011-08-15 6 views
0

------ main.c의 ---------세그멘테이션 결함 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 
+2

디버거에서 코드를 실행하는 경우 seg-fault가 발생하는 행은 무엇입니까? –

+2

포트란 배열은 1 또는 0 기반입니까? (수사적 질문이 아닙니다.) – zwol

+0

프로그램 수신 신호 EXC_BAD_ACCESS, 메모리에 액세스 할 수 없습니다. 이유 : 주소 : KERN_INVALID_ADDRESS 주소 : 0x0000000a00000df3 0x0000000a00000df3 ??() (gdb) 여기서 # 0 0x0000000a00000df3 ??() 주소 0xa00000df3의 메모리에 액세스 할 수 없습니다. # 1 0x0000000100000d0c start() – tracius01

답변

2

귀하의 C 코드는 이것이다 :

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

subrutine_fortran(&array,&size_a); 

및 포트란 코드는 이것이다 :

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 배열은 (C와 같이 다른 곳에서 온 경우에도) 1 색인으로 색인됩니다. 그래서 이것은 1에서 시작해야합니다. 또한 0이 맞으면 크기가 틀립니다. 너 같은 것을 원해.

do concurrent (i=1:h) 

그 변화와 함께, 그것은 나를 위해 작동합니다.

+0

고마워요, 지금 바보 같아서요 ... – tracius01

+0

Fortran 배열은 * knowing *에 대한 크레딧을 주장하고 싶지 않습니다. 1 기반. 어딘가에 읽은 기억이 희박하다. – zwol

+0

당신이 먼저 가져 왔습니다, 어느쪽으로 든 ... –