在C和C ++中,有许多操作会导致未定义的行为,即允许编译器执行所需操作的情况。示例包括在取消分配变量后使用变量,两次取消分配变量以及取消引用空指针。
Fortran是否也具有未定义的行为?我看了一份规范草案,但未能在其中找到任何东西。例如,在释放变量之后使用变量肯定会导致程序崩溃,还是它会默默地做错事?
我最近遇到了如何比较Fortran中的两个指针的问题.在C中,可以比较两个指针(pA == pB)(带pA和pB指针),因为它们只是地址.但是fortran指针不仅仅是纯内存地址.代码if(pa.ne.pb)(带有pa和pb是同一类型的指针)给了我一个错误
比较运算符'.ne.'的操作数 at(1)是TYPE(sometype)/ TYPE(sometype)
其中sometype是指针指向的类型.
有没有办法比较两个指针是否指向同一个目标?或者我是否必须为.ne.指向的类型创建一个-operator?
我想从子例程内部检测到传递的虚拟参数intent(in)实际上是空指针:
program testPTR
implicit none
integer, target :: ii
integer, pointer :: iPtr
iPtr => ii
iPtr = 2
print *, "passing ii"
call pointer_detect(ii)
print *, "passing iPtr"
call pointer_detect(iPtr)
iPtr => null()
print *, "passing iPtr => null()"
call pointer_detect(iPtr)
contains
subroutine pointer_detect(iVal)
implicit none
integer, intent(in), target :: iVal
integer, pointer :: iPtr
character(len = *), parameter :: sub_name = 'pointer_detect'
iPtr => iVal
if (associated(iPtr)) then
print *, "Pointer associated. Val=", iVal, ", iPtr …Run Code Online (Sandbox Code Playgroud)