用gfortran编译的神秘关联行为 [英] mysterious associated behavior compiled with gfortran

查看:62
本文介绍了用gfortran编译的神秘关联行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简短的fortran代码.

I have the following short fortran code.

  !==============================================
  MODULE PREC
  INTEGER, PARAMETER :: q=8
  END MODULE PREC

  !==============================================
  MODULE MOD_FIT
  USE prec         ! q

  TYPE spec
    INTEGER HL,HR
    COMPLEX(q), POINTER :: HMAT(:,:)   ! (HL,HR)
  END TYPE

  END MODULE MOD_FIT

  !==============================================
  PROGRAM MAIN
  USE prec
  USE MOD_FIT      ! spec
  IMPLICIT NONE
  !
  TYPE(spec) SMP

  write(*,*)'check associated:',associated(SMP%HMAT)

  END

我使用最新版本的gfortran对其进行了编译,然后运行了它.以下是我所得到的

I compiled it with the newest version gfortran, and ran it. The following is what I got

关联检查:T

应该是F,因为我根本没有初始化它吗?

Should it be F as I hadn't initialize it at all?

推荐答案

否,指针的状态为未定义.您不允许使用associated()进行查询,因为它可能导致任何结果.

No, the status of your pointer is undefined. You are not allowed to inquire it using associated() because it can result in anything.

您应该始终做的是对所有指针组件使用默认初始化并将其初始化为null().

What you should always do is to use default initialization of all pointer components and initialize them to null().

TYPE spec
    COMPLEX(q), POINTER :: HMAT(:,:) => null()
END TYPE

此后,您可以保证获得预期的结果 false .

After that you are guaranteed to get the expected result false.

这篇关于用gfortran编译的神秘关联行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆