https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114612

            Bug ID: 114612
           Summary: Deep copy missing for allocatable derived type
                    components
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vterzi1996 at gmail dot com
  Target Milestone: ---

Consider the following minimal example:
```
program prog
    type node
        integer::val
        type(node),allocatable::next
    end type
    type(node)::a,b
    allocate(a%next)
    a%val=1
    a%next%val=2
    print*,a%val,a%next%val
    b=a
    b%val=3
    b%next%val=4
    print*,a%val,a%next%val
    deallocate(b%next)
    print*,a%val,a%next%val
end
```
The output of the code compiled with the GNU (gfortran-13.1.0) compiler is as
follows:
```
           1           2
           1           4
           1           0
```
While the output of the code compiled with the Intel (ifx-2024.1.0) or NAG
(nagfor-7.1) compiler is different:
```
           1           2
           1           2
           1           2
```

It seems like gfortran treats allocatable derived type components somewhat as
pointers: after assignment `b=a`, the memory addresses of `a%next` and `b%next`
are the same, and you can change `a%next%val` by changing `b%next%val`.
However, deallocation of `b%next` will not deallocate `a%next` and will only
change its content.

In contrast to this behavior, the Intel and NAG compilers perform a deep copy
in the assignment `b=a` allocating new memory for elements of b.

The question to this example was asked here:
https://stackoverflow.com/questions/78282004/are-allocatable-derived-type-components-just-pointers-for-gnu-compiler?noredirect=1#comment138008854_78282004
and received an answer that this result is a bug of `gfortran`.

Reply via email to