- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我从这个答案( Fortran copy of pointer )中了解到,我应该尝试使用可分配数组而不是数组指针作为派生类型的组件。我打算这样做,但明年我会被锁定在当前的代码中,并且需要能够使用它并理解它,直到明年我可以做出更大更好的更改。
我还认为这个问题具有一定的普遍意义,因为我认为 Fortran 的行为虽然显然是正确的,但相当不直观。因此,更好地理解它的工作原理和原因可能很有值(value)(否则我认为编译器根本不会让我们这样做,对吧?)。
抱歉,介绍有点长。我将尝试将其作为一个已尽可能精简的带注释的程序来完成:
program main
type tRet
real :: agi
real :: wages_tgt(5) ! compiler won't let me declare as
! target but later I point to this
real, pointer :: wages(:)
end type tRet
type(tRet), target :: ret ! I don't quite understand
type(tRet), target :: orig1, orig2 ! why but compiler insists
! these be targets
ret%wages => ret%wages_tgt(1:1)
ret%wages = (/ 11. /)
orig1 = ret
ret%wages = (/ 99. /)
orig2 = ret
这是程序的上半部分,下面是一些结果,打印输出显示为右侧的注释:
! This is not want I want and I was surprised, but it is kind
! of explained in the other answer why this happens, so OK...
print *, "orig1%wages ", orig1%wages ! 99.
print *, "orig2%wages ", orig2%wages ! 99.
! But if I copy from orig1 or orig2 into ret then it
! works like I wanted it to work in the first place!
! But I don't completely understand why it works...
ret = orig1
print *, "ret%wages ", ret%wages ! 11.
print *, "orig1%wages ", orig1%wages ! 11.
print *, "orig2%wages ", orig2%wages ! 11.
ret = orig2
print *, "ret = orig2 "
print *, "ret%wages ", ret%wages ! 99.
print *, "orig1%wages ", orig1%wages ! 99.
print *, "orig2%wages ", orig2%wages ! 99.
end program main
我很高兴对这里发生的事情有任何很好的解释。我想,讽刺的是,我并不那么担心为什么这是一个坏主意,而是为什么我的解决方法似乎工作正常?
或者总结我的问题的最简单方法可能是:什么到底指向什么?
编译器:GNU Fortran (GCC) 4.8.5 20150623(Red Hat 4.8.5-16)
最佳答案
这里发生的情况是,当您复制派生数据类型时,派生类型的每个组件都会发生不同的情况。当您执行orig1 = ret
时:
wages_tgt
)会被分配新值。这相当于说 orig1%wages_tgt = ret%wages_tgt
。这些数组中的每一个都在内存中占据单独位置。工资
之类的指针,被设置为指向源指针当前指向的位置。这相当于说orig1%wages => ret%wages
。这两个指针的目标是内存中的相同位置。在此示例中,任何 wages
所指向的内存中的唯一位置是 ret%wages_tgt
。考虑到这一点,你的结果对我来说很有意义。添加一些选择性的附加注释:
ret%wages => ret%wages_tgt(1:1)
ret%wages = (/ 11. /) ! so wages_tgt = 11 also
orig1 = ret ! This is BOTH a copy and re-point
! * orig1%wages_tgt = ret%wages_tgt (11)
! * orig1%wages => ret%wages_tgt
ret%wages = (/ 99. /) ! this changes ret%wages & ret%wages_tgt
! to 99 and also orig1%wages since it is
! also pointing to ret%wages_tgt.
! note that orig1%wages_tgt is UNCHANGED
! (still 11) but nothing is pointing to it!
下面的代码...
ret = orig1 ! ret%wages_tgt = orig1%wages_tgt (11)
! no repointing actually happens this time b/c we have
! set up a circular relationship between all the
! pointers such that ALL of them point to ret%wages_tgt
关于fortran - 作为派生类型组件的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48675618/
是的,我知道..,这不是想象的...这是一个真正的 Fortran 问题。 以前的版本是指 Fortran 2003、95、90,甚至 77。 我所说的“向后兼容”是指可以轻松运行为 2008 年以前
我有一个程序,它的变量中有一个值。一旦确定了该值,我想调用另一个程序并使用该变量的值来确定在新程序中的位置。有人知道该怎么做吗? 最佳答案 如果您有 Fortran 2008 编译器,您将拥有标准子例
namelist 是一种有用的 fortran 结构,可以从文件中快速初始化变量。 namelist 有一个名称并包含一组具有已知类型的变量。这使得它类似于 type 结构。 通常情况下,给程序或子例
我正在遍历索引,我正在检查我是否不在第一个循环交互和另一个条件中。如果第一个条件是 .False.,我不想评估第二个条件。 do i = 1, n if ( i /= 1 .and. var(
Fortran 2003 具有用于数组连接的方括号语法,Intel fortran 编译器也支持它。我在这里为矩阵连接写了一个简单的代码: program matrix implicit none r
我正在尝试通过重载类型名称来制作自定义数据类型构造函数。但是,在进行调用时,将调用默认构造函数。我不明白我做错了什么。 这是有问题的代码片段。 module test type, pu
我的最终目标是在 Fortran 中有一个通用的映射函数,即一个接受任意类型 A 的数组和一个 A->B 类型的函数的函数,将此函数应用于给定数组的所有元素并返回一个B 类型的数组。我无法用数组实现它
我正在学习 Fortran,在使用格式编写时发现了一些奇怪的东西(我使用的是 Fortran onlinegdb) Program Hello real, dimension(3,2):: array
Fortran 中的INTERFACE 语句是否使其成为正式实现multiple dispatch 的编程语言? ? (我问是因为所链接的维基百科文章在其看似全面的支持相关范式的示例编程语言列表中并未
我可以使用 Fortran 95 编译器编译 Fortran 90 文件吗? Fortran 95 似乎有很多,但 Fortran 90 没有。 最佳答案 这个可以: NAGWare f95 Comp
嗨,我在 Fortran 中对二维离散化问题强加边界条件时遇到了麻烦。我的离散化网格是一个二维正方形,在 x,y 方向上从 -L 到 L。 我想强加这样的边界条件, 在 x=L 的边界线上,指定了函数
Fortran 是否有与 C assert 等效的标准函数/关键字? ? 我找不到 assert我在Fortran2003标准中提到过。我发现了一些如何使用预处理器的方法,但是在这个 answer建议
我有一系列的作业,使用“;”将它们分配给同一个ike。分开statemnts,但我收到此错误: 1.0;磅(1,9) 1个 错误:(1)处无法分类的陈述 在文件LJ.F90:223中 如果每个语句都在
我正在使用 gfortran -std=f2008。我有一个函数,它返回一个包含可分配数组的派生类型。该函数在返回之前调用allocate()。似乎在分配数组的函数返回之后,数组会自动释放一段时间,并
我制作了这个小型测试程序来“证明”在编译之前(或者如果你让它们可分配),你不能在不指定它们的大小的情况下使用向量。我的观点失败了。我期待本地向量“num”会失败。程序在执行程序之前无法知道它的大小。大
出于优化原因,Fortran 强制子例程或函数的虚拟参数不是别名,即它们不指向相同的内存位置。 我想知道相同的约束是否适用于函数的返回值。 换句话说,对于给定的 myfunc 函数: function
我已经在Fortran 90中编写了一个相当大的程序。它已经运行了一段时间了,但是今天我尝试将其提高一个档次并增加问题的大小(这是研究非标准的有限元求解器,如果那样的话)。可以帮助任何人...)现在,
在 C 和 C++ 中,有许多操作会导致未定义的行为,即允许编译器做任何它想做的事情的情况。 Examples包括在释放变量后使用它,释放变量两次和取消引用空指针。 Fortran 是否也有未定义的行
通常我使用fortran进行数值分析,然后使用matlab、R和python进行后期和前期工作。 我发现 matlab、R 和 python 在终端中提供了命令提示符,以便您可以运行脚本以及从命令行立
在 Fortran 中将变量设置为 +Infinity 的最安全方法是什么?目前我正在使用: program test implicit none print *,infinity() con
我是一名优秀的程序员,十分优秀!