recursiv deallocation

recursiv deallocation

Hello,

I have a type with a pointer to elements of the same type, which are meant as some kind of children.
No I want to have the ability to deallocate an element with all its children. The following code shows the problem:

module CONTAINER
  type :: t_con
     type(t_con), pointer   :: sub_con(:)
     character(20), pointer :: string(:)
  end type t_con
contains
  recursive subroutine dealloc(con)
    implicit none
    type(t_con)             :: con
    integer                 :: con_no
    
    write(0,"('Entering dealloc')", advance='no')
    if (associated(con%string)) then
       write(0,"(' name: ', A10)", advance='no') con%string(1)
       deallocate(con%string)
    end if
    write(0,*)
    if (associated(con%sub_con)) then
       do con_no = 1, size(con%sub_con)
          call dealloc(con%sub_con(con_no))
       end do
    end if
  end subroutine dealloc
end module CONTAINER

program test
  use CONTAINER
  implicit none
  character(20)    :: test_string
  type(t_con)      :: con
  integer          :: sub_con_no

  allocate( con%sub_con(5) )
  allocate( con%string(1) )
  con%string(1) = 'parent'
  do sub_con_no = 1, 4
     allocate(con%sub_con(sub_con_no)%string(1))
     write(con%sub_con(sub_con_no)%string(1), "('child ', I1)") sub_con_no
  end do
  call dealloc(con)
end program test

The output is:

wiefer@ferdinand:~/uni/util> ./a.out
Entering dealloc name: parent
Entering dealloc name: child 1
Entering dealloc name: child 2
Entering dealloc name: child 3
Entering dealloc name: child 4
Entering dealloc

There is one element containing 5 children ("sub_con"). The first 4 are filled with a text, in the fifth one "string" is neither allocated nor associated.
Nevertheless the ( associated(string) ) seems to be .true..
Does anybody know why?

Version:

wiefer@ferdinand:~/uni/util> ifc -V
Intel Fortran Compiler for 32-bit applications, Version 7.1   Build 20030307Z
Copyright (C) 1985-2003 Intel Corporation.  All rights reserved.
FOR NON-COMMERCIAL USE ONLY

GNU ld version 2.13.90.0.18 20030121 (SuSE Linux)
  Supported emulations:
   elf_i386
   i386linux
   elf_x86_64

Thanks,
Juergen

1 post / 0 new
For more complete information about compiler optimizations, see our Optimization Notice.