分段错误 C 和 Fortran

发布于 2024-11-30 02:10:38 字数 1497 浏览 0 评论 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

------ 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

When I do a loop through array elements:

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

I get a segmentation fault.

It doesn't happen when I write:

array[3]=array[3]+1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿咻 2024-12-07 02:10:38

你的 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 索引的(即使它们来自其他地方,比如 C)。所以应该从 1 开始。此外,如果 0 是正确的,则大小将是错误的。你想要这样

    do concurrent (i=1:h)

的改变,它对我有用。

Your C-code is this:

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

subrutine_fortran(&array,&size_a);

and your Fortran code is this:

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

This is wrong a couple of ways - as Zack points out, Fortran arrays are 1-indexed (even when they come from somewhere else, like C). So this should start at 1. Also, if 0 was right, the size would be wrong. You want something like

    do concurrent (i=1:h)

With that change, it works for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文