首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Fortran中正确执行最终例程

在Fortran中正确执行最终例程
EN

Stack Overflow用户
提问于 2018-12-27 21:48:38
回答 1查看 200关注 0票数 1

我有以下Fortran代码

代码语言:javascript
复制
  type t_octree
     integer                        :: max_num_point
     class(t_octree_node), pointer  :: root => null()
     contains
     final                         :: DESTROY_OCTREE
  end type t_octree

  type t_octree_node
     real                          :: box(2,3)
     integer                       :: depth
     type(t_octree_node), pointer  :: parent      => null()
     type(t_octree_node), pointer  :: children(:) => null()
     class(t_octree_leaf), pointer :: leaf        => null()
  contains
     final                         :: CLEAN_NODE
  end type t_octree_node
  type, extends(t_octree_node) :: t_octree_leaf
     integer, allocatable          :: id(:)
     integer                       :: num_point
  contains
     final                          :: DESTROY_LEAF
  end type

在我完成了我的处理之后,现在需要确保我的东西被正确的分配了。

在我最后的例行公事中

代码语言:javascript
复制
  subroutine DESTROY_OCTREE( this )

  implicit none

  type(t_octree)               :: this
  type(t_octree_node), pointer :: node => null()
  integer                      :: i


  node => this% root
  if( associated(node% leaf) )deallocate( node% leaf )

  if(.not. associated(node% children) )RETURN
  deallocate(  node )
  node => this% root
  i = 0
  nullify(node)
  print*, associated(this% root) ! this is true!! 
  print*, associated(this% root% children) ! this is true!! 



  end subroutine DESTROY_OCTREE

  recursive subroutine CLEAN_NODE ( this )

  implicit none

  type(t_octree_node)           :: this
  type(t_octree_node), pointer  :: node => null(), next => null()
  integer                       :: i

  if( associated(this% leaf) )then
     deallocate( this% leaf )
     nullify( this% leaf )
  endif
  if(.not. associated(this% children) )RETURN
  do i = 1, 8
     node => this% children(i)
     deallocate( node )
     nullify(node)
    ! debug
    ! print*, i, "rec"
  enddo
   nullify(this% children)
   nullify(this% parent)

  end subroutine CLEAN_NODE

  subroutine DESTROY_LEAF ( leaf )

  implicit none

  type(t_octree_leaf)        :: leaf

  deallocate( leaf% id )

  end subroutine DESTROY_LEAF

在我的主程序中,我执行以下操作

代码语言:javascript
复制
  program TEST_OCTREE
  use MD_OCTREE

  implicit none

  type(t_octree), pointer               :: octree

  octree      => t_octree( max_num_point,  box )
  (...) ! all processing to build the data structure

现在我通过简单的

代码语言:javascript
复制
  deallocate( octree )  
  print*, associated(octree% root) ! this give a segmentation fault

问题可以解释为什么我的print*, associated(this% root)命令在打印我的主程序时仍然显示TRUE,因为它给了我一个分段错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-27 22:48:41

取消分配指针会导致与同一目标关联的任何其他指针的指针关联状态变为未定义(F2018 9.7.3.3p2)。这种情况发生在nodethis%root过程中--两个指针引用同一个对象(通过指针赋值node => this% root,对象通过node指针被解除分配),这使得this%root具有未定义的关联状态。

关联内部的参数不能是具有未定义关联状态的指针(F2018 16.9.16p3)。代码是不符合的,任何事情都可能发生--其中“任何事情”都相当合理地包括了你所看到的结果。

当您通过node指针释放对象时,没有简单的方法可以让处理器可靠地更新this%root指针的状态--它最终指向的是不再存在的东西。

在显示的源片段中还有其他可疑结构,包括在同一个指针对象上的DEALLOCATE语句之后使用多余的空值(成功地解除分配不关联(无效)指针),在DEALLOCATE可能合适时使用NULLIFY (很难说没有完整的代码),以及使用结构构造函数作为指针目标。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53951210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档