【发布时间】:2014-11-12 11:32:39
【问题描述】:
基础:我正在尝试使用构造函数和析构函数在 Fortran 中编写好的代码。
这是一个非常简单的Test 类的示例,它是客户端:
module test_class_module
implicit none
type :: Test
private
integer, allocatable :: arr(:)
CONTAINS
final :: destructor
end type
interface Test
procedure :: constructor
end interface
CONTAINS
function constructor(arr_size) result(this)
type(Test) :: this
integer, intent(in) :: arr_size
write(*,*) 'Constructor works'
allocate(this % arr(arr_size))
end function
subroutine destructor(this)
type(Test) :: this
write(*,*) 'Destructor works'
if (ALLOCATED(this % arr)) deallocate(this % arr)
end subroutine
end module
program test_client
use test_class_module
type(Test) :: tst
tst = Test(100)
end
问题:
我用valgrind 运行它并打印出来:
Constructor works
Destructor works
Destructor works
==22229== HEAP SUMMARY:
==22229== in use at exit: 432 bytes in 2 blocks
==22229== total heap usage: 10 allocs, 8 frees, 13,495 bytes allocated
问题:为什么还要分配内存? (P.S. 我理解需要赋值运算符才能正确使用类,但这对于这个问题是不够的)感谢您的任何想法。
【问题讨论】:
标签: memory-management constructor fortran valgrind destructor