内存泄漏使用相同的指针指向众多目标

时间:2018-06-02 21:07:31

标签: fortran intel-fortran

我的IDE是带有集成英特尔Fortran编译器的Visual Studio 2010。编译器的版本是:Intel Parallel Studio XE 2011。

我在Fortran中没有经验丰富的程序员,所以我需要一些帮助来使用指针来读取.txt文件中的数据。这是我的示例代码:

Module Derived_type

implicit none

Type , public :: Something

  private

       Real :: Somth_1
    Integer :: Somth_2

  contains

    procedure , public :: read_input => read_data_input
    procedure , public :: t_Somth_1 => t_data_Somth_1
    procedure , public :: t_Somth_2 => t_data_Somth_2

End Type Something

private :: read_data_input
private :: t_data_Somth_1 , t_data_Somth_2

contains

Subroutine read_data_input( This , Un_r )

  Class( Something ) :: This
  Integer , intent( in ) :: Un_r

  Read ( Un_r , * , Err = 100 ) This%Somth_1
  Read ( Un_r , * , Err = 101 ) This%Somth_2

Return

100 Stop ( "Read format error - 100 !!!" )
101 Stop ( "Read format error - 101 !!!" )

End Subroutine read_data_input

Function t_data_Somth_1 ( This ) result( data_Somth_1 )

  Class( Something ) :: This
  Real :: data_Somth_1

  data_Somth_1 = This%Somth_1

End Function t_data_Somth_1

Function t_data_Somth_2 ( This ) result( data_Somth_2 )

  Class( Something ) :: This
  Integer :: data_Somth_2

  data_Somth_2 = This%Somth_2

End Function t_data_Somth_2

End Module Derived_type

Program Memory_leaking

Use , non_intrinsic :: Derived_type

Implicit none

Integer :: i , alloc_err , dealloc_err
Integer , parameter :: N_snv = 3
Character( 256 ) :: Name
Character(*),parameter :: a00 = '("Input_",i1,".txt")'

Class( Something ) , pointer :: Po_Something

Type( Something ) , allocatable , target :: Tar_Something(:)

! Memory allocation

allocate ( Po_Something , Stat = alloc_err )

If ( alloc_err .ne. 0 ) Stop ( "Allocation wrong - Po_Something !!!")

If ( .not. allocated ( Tar_Something ) ) allocate( Tar_Something( N_snv ) , stat = alloc_err )

If ( alloc_err .ne. 0 ) Stop ( "Allocation wrong - Tar_Something !!!")


Do i = 1 , N_snv

   Po_Something => Tar_Something(i)

   Write( Name , a00 ) i

   Open( 15 , File = Name , Status = 'Unknown' , Action = 'Read' )

      Call Po_Something%read_input( 15 )

   Close( 15 , Status = 'Keep' )

   Write(*,*) Po_Something%t_Somth_1() , Po_Something%t_Somth_2()

End Do

! Memory deallocation

deallocate ( Po_Something , Stat = dealloc_err )

If ( dealloc_err .ne. 0 ) Stop ( "deAllocation wrong - Po_Something !!!")

If ( allocated ( Tar_Something ) ) deallocate( Tar_Something, stat = dealloc_err )

If ( dealloc_err .ne. 0 ) Stop ( "deAllocation wrong - Tar_Something !!!")

End program Memory_leaking

我有一个派生类型的数组,我想使用相同的指针为每个单独的数组读取.txt文件的数据,就像在我的示例代码中一样。 我需要打破连接beatwean指针和目标进行循环执行吗? 在这种情况下是否有内存泄漏?

1 个答案:

答案 0 :(得分:1)

是的,你有内存泄漏。

allocate ( Po_Something , Stat = alloc_err )

在这里,您手动为指针变量分配存储并指向其内存地址。

Po_Something => Tar_Something(i)

之后,您将poitee更改为另一个存储(可分配变量)并留下您之前分配的存储。在此指针关联之后,不再有任何引用指向您手动分配的地址。该程序在结束时无法解除分配。

由于您没有使用第一次分配的存储,因此这里的解决方案就是不这样做。指针将指向已在循环中分配的内存。

可自动释放可分配变量(在现代Fortran中),但是在更改其目标之前,需要手动取消分配为指针变量手动分配的任何内存。

检查我的其他答案,了解何时需要手动释放以供参考。 When is array deallocating necessary?

(更好的是,下载Fortran标准的副本以供参考https://wg5-fortran.org