Calling a global string variable from Fortran in C causes segmentation fault

…衆ロ難τιáo~ 提交于 2019-12-24 06:38:22

问题


I'm trying to call a global string variable which is defined in a Fortran subroutine, in C. the C code is Cfile.c:

#include <stdio.h>

typedef struct {
        int length;
        char* string;
} fstring;

extern fstring stringf_;
void fortfunc_();

int main() {
        fstring stringC = stringf_;
        stringC.string[stringC.length-1] = '\0';
        printf("%s \n",stringC.string);
        return 0;
}

and FORTRAN code is Ffile.f:

subroutine fortfunc()

        character*30 string
        common/stringF/ string
        string = 'this is a string in FROTRAN77'

return
end

it compiles with:

gcc -c Cfile.c
gfortran -c -std=legacy Ffile.f
gfortran -c file.out -std=legacy Cfile.o Ffile.o

but when running I get the segmentation fault. I do not understand when I'm violating the memory boundaries though.

My operating system is:

Linux ubuntu 4.15.0-39-generic #42-Ubuntu SMP Tue Oct 23 15:48:01 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

and my compilers are:

GNU Fortran (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0

gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0

I would appreciate if you could help me know where is my mistake and how I can solve it? other solutions to define a global variable in Fortran and then calling it in C are also welcomed.


回答1:


Based on the comments I got here and on Reddit, I now have a code which works. The C code:

#include <stdio.h>

typedef struct {
    char s[30];
} fstring;

extern fstring stringf_;

int main() {
    fstring stringc = stringf_;
    stringc.s[29] = '\0';
    printf("%s\n",stringc.s);
    return 0;
}

and FORTRAN code:

        BLOCK DATA

                CHARACTER*30 S
                COMMON /STRINGF/ S
                DATA S /'this is a string in FROTRAN77'/

        end

The segfault was happening because the passed stringC.length value is zero. It means unlike the example I was following here when calling a string from the FORTRAN side it does not pass the length as an integer!



来源:https://stackoverflow.com/questions/53567166/calling-a-global-string-variable-from-fortran-in-c-causes-segmentation-fault

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!