Defining a pointer component auto-pointed to a module variable

Hello,

I remember I read somewhere (on the net) that it is possible to define a pointer component that is pointed to some other module variable via compile-time initialization, for example:

module test_m
    implicit none

    integer, target :: arr_global( 3 )

    type Mytype
        integer, pointer :: arr(:) => arr_global
    endtype

end module

program main
    use test_m
    implicit none
    type(Mytype) :: m1, m2

    m1 % arr(:) = [1,2,3]    !! segmentation fault

    !! print *, m2 % arr(:)
end

However, although the above code compiles with gfortran-11 (on Ubuntu22), it fails with segmentation fault (invalid memory reference). Is my syntax above incorrect, or am I doing something invalid…?

Your code compiles and works correctly with nagfor. I activated the print statement, and I get:

$ nagfor cpoint.f90 && a.out
NAG Fortran Compiler Release 7.1(Hanzomon) Build 7114
[NAG Fortran Compiler normal termination]
 1 2 3
1 Like

It works in ifort/ifx as well.

1 Like

@septc, can you please share what fraction of your work involves Fortran and how much of it depends on gfortran? With your expertise with different languages, perhaps you can start submitting gfortran issues at GCC Bugzilla and, more importantly, develop and submit patches to the bugs?

Given your apparent use of gfortran on Linux and/or macOS, and also what appears to be those of several other readers here at Fortran Discourse, gfortran users can do with becoming active developers of gfortran. The gfortran-specific issue you’ve uncovered here is a perfect example.

1 Like