File: intent_out_allocatable_struct_with_components.f90

package info (click to toggle)
lfortran 0.59.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 56,736 kB
  • sloc: cpp: 168,052; f90: 74,272; python: 17,537; ansic: 7,705; yacc: 2,345; sh: 1,334; fortran: 895; makefile: 37; javascript: 15
file content (35 lines) | stat: -rw-r--r-- 982 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
program test_allocatable_struct_with_allocatable_components
    implicit none
    type :: container_t
        integer, allocatable :: data(:)
    end type
    type(container_t), allocatable :: arg

    ! Initial allocation
    allocate(arg)
    allocate(arg%data(5))
    arg%data = [1, 2, 3, 4, 5]

    ! Call with intent(out) - must deallocate arg on entry
    call process(arg)

    ! Verify re-allocation worked
    if (.not. allocated(arg)) error stop "arg should be allocated after call"
    if (size(arg%data) /= 3) error stop "wrong size"
    if (any(arg%data /= [10, 20, 30])) error stop "wrong data"

    print *, "PASS"

contains
    subroutine process(x)
        type(container_t), allocatable, intent(out) :: x

        ! Intent(out) must deallocate on entry
        if (allocated(x)) error stop "intent(out) must deallocate on entry"

        ! Allocate fresh
        allocate(x)
        allocate(x%data(3))
        x%data = [10, 20, 30]
    end subroutine
end program