unset_next Subroutine

private subroutine unset_next(this, deallocate_old)

Arguments

Type IntentOptional AttributesName
class(linked_node), intent(inout) :: this
logical, intent(in), optional :: deallocate_old

Whether to deallocate (rather than just nullify) any existing subsequent nodes in the chain. Defaults to .false..

Description

Unsets the pointer to the next node in the chain, severing it. By default, the pointer is only nullified. This may result in a memory leak. Optionally, by setting deallocate_old=.true., the next node (and all nodes it points to) can be deallocated. This may result in a segfault if another part of the program tries to access the former next node.


Source Code

  subroutine unset_next(this, deallocate_old)
    !* Author: Chris MacMackin
    !  Date: February 2016
    ! 
    ! Unsets the pointer to the next node in the chain, severing it.
    ! By default, the pointer is only nullified. This may result in a
    ! memory leak. Optionally, by setting `deallocate_old=.true.`, the
    ! next node (and all nodes it points to) can be deallocated. This
    ! may result in a segfault if another part of the program tries to
    ! access the former next node.
    !
    class(linked_node), intent(inout) :: this
    logical, optional, intent(in) :: deallocate_old
      !! Whether to deallocate (rather than just nullify) any existing
      !! subsequent nodes in the chain. Defaults to `.false.`.
    if (.not. this%has_next()) return
    if (present(deallocate_old)) then
      if (deallocate_old) then
        call this%next%unset_next(.true.)
        deallocate(this%next)
        return
      end if
    end if
    nullify(this%next)
  end subroutine unset_next