“n = n”的目的是什么?
我正在阅读一些 Fortran 代码,以前的程序员经常会抛出“n = n”语句。这样做的目的是什么?一些示例代码:
if (cmult.lt.5.) then
kx = 0
do k=ipd(ii),lpd(ii)
kx = kx + 1
p1(kx) = epp_rfc(ipp,k)
epp_rfc(ipp,k) = cmult*epp_rfc(ipp,k) + x
zero(ix)
p2(kx) = epp_rfc(ipp,k)
n = n
enddo
if (cmult.gt.0.) then
n = n
endif
else
nk = lpd(ii) - ipd(ii) + 1
do k=ipd(ii),lpd(ii)
kx = kx + 1
p1(kx) = epp_rfc(ipp,k)
epp_rfc(ipp,k) = pp(imem) + zero(ix)
p2(kx) = epp_rfc(ipp,k)
n = n
enddo
endif
I am reading some Fortran code, and every so often the previous programmer throws in the statement 'n = n'. What is the purpose of this? Some example code:
if (cmult.lt.5.) then
kx = 0
do k=ipd(ii),lpd(ii)
kx = kx + 1
p1(kx) = epp_rfc(ipp,k)
epp_rfc(ipp,k) = cmult*epp_rfc(ipp,k) + x
zero(ix)
p2(kx) = epp_rfc(ipp,k)
n = n
enddo
if (cmult.gt.0.) then
n = n
endif
else
nk = lpd(ii) - ipd(ii) + 1
do k=ipd(ii),lpd(ii)
kx = kx + 1
p1(kx) = epp_rfc(ipp,k)
epp_rfc(ipp,k) = pp(imem) + zero(ix)
p2(kx) = epp_rfc(ipp,k)
n = n
enddo
endif
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样的代码经常用于允许程序员在不支持条件断点的调试器中设置断点。
通过在该行上设置断点,只有
cmult.gt.0.
时才会命中该断点。Code like this is frequently used to allow the programmer to set a breakpoint in debuggers that don't support conditional breakpoints.
By setting a breakpoint on that line, it will only be hit if
cmult.gt.0.
n = n
用于检查cmult.gt.0.
的值并用作“条件断点”这一事实在某种意义上是巧合的,因为它不是上面代码片段中出现 n = n 的原因。在这种情况下使用 n = n 的真正原因在于,源代码中的作用域从未被最流行的编译器转换为汇编语言(计算机不够聪明,无法理解“作用域”是什么,除非它明确地由程序员编程,如果这样做,程序的运行速度将慢 100 秒……因为为支持范围界定的逻辑发出了额外的指令)。它只是在源代码中限制程序员并向代码引入一种结构,以便链接器和链接器可以使用它。编译器可以完成他们的工作 - 或者如果违反范围则发出错误。
由于范围不存在,
enddo
无法暂停,除非编译器小心地插入一些指令(如 nop)和调试符号,以神奇地允许调试器在enddo
处停止代码>.如果没有 n = n,则无法检查此行p2(kx) = epp_rfc(ipp,k)
的结果,因为值在循环顶部重置。这就是为什么使用 n = n 在p2(kx) = epp_rfc(ipp,k)
之后停止并检查每次迭代的最终结果。在它之后,当出现以下代码时:用于方便(同样,您不能在 endif 或 else 处停止)目的,是的,这是一种条件断点。但是 n = n 再次被使用,因为你不能在检查
if (cmult.gt.0.)
之后停止 - 你可以在它处停止,但是不在之后 - 下一条指令将在外部 if else 之后。我希望这是有道理的。编辑:如果仍然没有太多意义,这里有附加信息:为了检查
p2(kx) 之后
已执行,调试器必须发出指令来检查/获取它 - 因此需要知道 a) p2(kx) b 的大小是多少)它的位置 - 记住最后一个 指示)! c) 发出适当的指令来获取值p2(kx)
的值= epp_rfc(ipp,k)p2(kx)
。对于调试器来说,这一切都很复杂,因为这实际上是逻辑 - 调试器必须是“智能”的(属于人工智能领域),如果调试器可以做到这一点,那么终结者现在就已经存在了。The fact that
n = n
is used for checking the value ofcmult.gt.0.
and is used as 'condition breakpoint' is coincidental in a sense that it's not the reason why n = n appears in the above code snippet.The real reason why n = n is used in this case lies in the fact that scope in the source code is never translated by the most popular compilers to assembly language (the computer is not smart enough to understand what 'scope' is unless its explicitly programmed by the programmer and if it did the programs will run 100s of time slower ... because of the extra instructions emitted for the logic to support scoping). It's only there to constraint the programmer in the source code and introduce a structure to the code so that linker & compiler can do their job - or issue error if violate scoping.
Because scope doesn't exist
enddo
cannot be paused at unless the comipler takes care to insert some instruction(like nop) and debug symbols for it to magically allow the debugger to stop atenddo
. Without n = n the outcome of this linep2(kx) = epp_rfc(ipp,k)
cannot be checked as the values reset at the top of the loop. This is why n = n is used to stop after thep2(kx) = epp_rfc(ipp,k)
and check the final result on each itteration. After it when this code comes:is used for convenience (again you cannot stop at endif or else) purposes and yes this is kind of conditional breakpoint. But n = n is again used because you cannot stop after checking
if (cmult.gt.0.)
- you can stop at it but not after it - the next instruction will be after the outer if else. I hope this makes sense.Edit: If it still doesn't make too much sense here is additional info: in order to check the value of
p2(kx)
afterp2(kx) = epp_rfc(ipp,k)
has executed the debugger has to emit instructions to check/fetch it - hence it is required to know a) what is the size ofp2(kx)
b) it's location - remember the last instruction(s)! and c) emit the appropriate instructions to fetch the valuep2(kx)
. All to complicated for the debugger as this is actually logic - the debugger has to be 'smart' (falls into AI domain), if a debugger could do this Terminator would have existed by now.