首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将内部函数作为参数传递时的分割故障

将内部函数作为参数传递时的分割故障
EN

Stack Overflow用户
提问于 2018-04-22 12:39:40
回答 1查看 625关注 0票数 2

我有一些代码将主程序的内部函数作为参数传递给函数:当传递的函数最终被调用时,它会导致分段错误。只有当我在Linux上使用Windows子系统(我在WSL上使用Ubuntu 16 )时才会发生这种情况;在本地Linux或Mac机器上运行时不会发生这种情况。

一个最小的崩溃示例:

代码语言:javascript
复制
module test1

implicit none

contains

subroutine x(ff,y)

    interface 
        real function ff(y)
            real, intent(in) :: y
        end function ff
    end interface
    real, intent(in) :: y
    integer z

    z=ff(y)

end subroutine x

end module test1

program tester
use test1
implicit none

call x(f,1.0)

contains

real function f(y)
    real, intent(in) :: y

    write(*,*) y
    f=y*y
end function f

end program tester

编撰:

代码语言:javascript
复制
 gfortran-7 -ggdb test_fun_passing.f90 -o test

回溯跟踪,gdb输出:

代码语言:javascript
复制
(gdb) bt                                                                                             
#0  0x00007ffffffde320 in ?? ()                                                                         
#1  0x0000000000400734 in test1::x (ff=0x7ffffffde320, y=1) at test_fun_passing.f90:17                  
#2  0x0000000000400829 in tester () at test_fun_passing.f90:31                                          
#3  0x0000000000400860 in main (argc=1, argv=0x7ffffffde64f) at test_fun_passing.f90:27                 
#4  0x00007ffffec70830 in __libc_start_main (main=0x40082c <main>, argc=1, argv=0x7ffffffde448,             init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7ffffffde438)        at ../csu/libc-start.c:291                                                                         

#5  0x0000000000400669 in _start ()                                                                     

这个程序确实可以工作(将f移动到它自己的模块中,但仍然作为参数传递),所以程序中包含了一些关于f的内容。

代码语言:javascript
复制
module test1

implicit none

contains

subroutine x(ff,y)

    interface 
        real function ff(y)
            real, intent(in) :: y
        end function ff
    end interface
    real, intent(in) :: y
    integer z

    z=ff(y)

end subroutine x

end module test1

module test2

implicit none

contains

real function f(y)
    real, intent(in) :: y

    write(*,*) y
    f=y*y
end function f

end module test2

program tester
use test1
use test2
implicit none

call x(f,1.0)

end program tester


gfortran-7 -ggdb test_fun_passing.f90 -o test && ./test                                                                                              
1.00000000        

以这种方式传递f是有效的Fortran,还是我在本地Linux上转发了一些非标准特性?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-25 14:38:21

看来我会遇到这样的情况:

https://github.com/Microsoft/WSL/issues/3083https://github.com/Microsoft/WSL/issues/286

WSL有一个不可执行的堆栈.跑步:

代码语言:javascript
复制
excestack -c test

在本机linux上,要从二进制文件中删除导出堆栈,就会触发与我在WSL上相同的错误消息。清除/设置WSL上的外挂堆栈(带有-c/-s)什么也不做。从github bug报告来看,它似乎不太可能被修复。

编辑:

似乎转移到WSL版本2解决了这个问题。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49965980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档