在C和C ++中,有许多操作会导致未定义的行为,即允许编译器执行所需操作的情况。示例包括在取消分配变量后使用变量,两次取消分配变量以及取消引用空指针。
Fortran是否也具有未定义的行为?我看了一份规范草案,但未能在其中找到任何东西。例如,在释放变量之后使用变量肯定会导致程序崩溃,还是它会默默地做错事?
我正在尝试使用 来学习 Fortran2018 gfortran。在使用指针时,我注意到似乎没有测试空指针的工具。所以我有两个问题:
更实际地说,在下面的代码片段中:我们如何在执行过程中的任何时刻找出分配给p是否“安全”?allocate(可能会想象一个更复杂的、nullify和语句序列deallocate。)
program test
implicit none
real, pointer :: p
! p = 333.333 ! Segfault, p is neither defined, nor allocated
! Since p is not defined, checking whether it's
! associated to a target gives arbitrary results:
print *, "Start: Pointer p associated? ", associated(p) ! Result: True or False
nullify(p) ! Now p is defined, but `null`
print *, "Nullyfied: …Run Code Online (Sandbox Code Playgroud)