Questions regarding derived data types with allocatable arrays

  • Follow


Hello,

If I have the following types in some module:

type my_type
  integer, private :: num1
  double precision, dimension (:), allocatable :: array1
end type my_type

type my_array_of_types
  integer, private :: num2
  type (my_type), dimension (:), allocatable :: array2
end type my_array_of_types


and I properly allocate all components (array2 + all the different
array1's) is it okay to deallocate the my_array_of_types array2
variable before deallocating each of the my_type array1 variables
without getting a memory leak? i.e.:
Deallocating all:
subroutine deallocate_my_array_of_types(this)
implicit none

! Input Variables
  type (my_array_of_types) :: this

! Local Variables
  integer i

  if(allocated(this%array2))then

    do i = 1, this%num2

      call deallocate_my_type(this%array2(i))

    end do
    deallocate(this%array2)
    this%num2 = 0

  end if

end subroutine deallocate_my_array_of_types

subroutine deallocate_my_type(this)
implicit none

! Input Variables
  type (my_type) :: this

  if(allocated(this%array1))then

    deallocate(this%array1)
    this%num1 = 0

  end if

end subroutine deallocate_my_type

Just Deallocating array2:
subroutine deallocate_my_array_of_types(this)
implicit none

! Input Variables
  type (my_array_of_types) :: this

! Local Variables
  integer i

  if(allocated(this%array2))then

    deallocate(this%array2)
    this%num2 = 0

  end if

end subroutine deallocate_my_array_of_types

My tests seem to show that deallocating all is redundant but I would
like to confirm before basing code on this assumption.

Also, if I choose to allocate this structure in a subroutine and pass
it out the values and structure seem to persist. As I understand it
this is different than say just an allocatable array that is allocated
and then passed out of a subroutine without an interface; is this
true?

Thanks,
Cyrus
0
Reply Cyrus 11/21/2010 12:23:03 AM

Cyrus <cyrusproctor@gmail.com> wrote:
[code elided]
> and I properly allocate all components (array2 + all the different
> array1's) is it okay to deallocate the my_array_of_types array2
> variable before deallocating each of the my_type array1 variables
> without getting a memory leak?

1. Yes.

2. Allocatables never leak memory. That is by design.  If you think you
found one somewhere then it is a bug. The bug could be one of 3 places,
most likely first

a. In something else in your code.

b. In the compiler.

c. In the standard.

The item (c) is because the standard was specifically designed so that
allocatables don't leak memory. If a case was missed, that would be a
bug in the standard. I don't know of any such bug at present, but I list
the possibility for completeness.

-- 
Richard Maine                    | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle           |  -- Mark Twain
0
Reply nospam 11/21/2010 6:50:21 AM


1 Replies
748 Views

(page loaded in 0.039 seconds)

Similiar Articles:













7/23/2012 6:03:09 AM


Reply: