Associated pointers in derived type? gFortran vs. Intel

后端 未结 1 1859
抹茶落季
抹茶落季 2020-12-19 03:36

I would like to check if a pointer inside a derived type has already been defined or not. I wrote the following simple code to show you my problem:

program          


        
相关标签:
1条回答
  • 2020-12-19 03:49

    You are testing to see if a pointer is associated without explicitly using nullify on the pointers. A great page on common Fortran mistakes remarks (with the code sample removed):

    Many people think that the status of a pointer which has never been associated is .not. associated. This is false. (...) When a pointer is declared its status is undefined, and cannot be safely queried with the associated intrinsic.

    It looks like the gfortran compiler may be set up to explicitly nullify pointers on declaration - you should probably think of this like the compiler automatically setting declared variables to zero, and not count on that behavior. If you want to be sure, you will nullify it yourself.

    Edit:

    I'm reading through the Intel compiler guide, and it specifies how to make sure that the pointer is nullified correctly - you can set up your derived type as

    type y
        real(8), pointer :: x(:) => null()
    end type y
    

    Note, however, that it seems like this is limited to Fortran 95, as mentioned in the linked article.

    0 讨论(0)
提交回复
热议问题