Fortran 是否有未定义的行为? [英] Does Fortran have undefined behavior?

查看:42
本文介绍了Fortran 是否有未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C 和 C++ 中,有许多操作会导致未定义的行为,即允许编译器做任何事情的情况.示例包括在释放变量后使用变量、释放变量两次以及取消引用空指针.

In C and C++, there many operations that cause undefined behavior, i.e. situations that allow the compiler to do anything it wants. Examples include using a variable after deallocating it, deallocating a variable twice and dereferencing null pointers.

Fortran 是否也有未定义的行为?我查看了规范草案,但没有在其中找到任何内容.例如,在释放后使用变量是否会导致程序崩溃,或者它可能会默默地做错事?

Does Fortran also have undefined behavior? I had a look at a specification draft, but failed to find anything in there. For instance, is using a variable after its deallocation guaranteed to crash the program, or may it silently do the wrong thing?

推荐答案

是的,它有.它只是叫法不同.您可以做很多事情,并且会使您的代码不符合标准,因此不需要处理器(编译器)诊断这种不符合(当然,许多偏差必须确诊).通常情况类似于 C 未定义行为的情况(例如访问数组越界、有符号整数溢出……).我们只是说代码不符合标准,这意味着标准没有规定这种代码的结果.此类代码未涵盖,但如果某些编译器(处理器)确实编译它并且您运行它,则可能会产生anything.

Yes, it has. It is just called differently. There are many things that you could do and will make your code not standard conforming, for which there is no requirement to for the processor (compiler) to diagnose such non-conformance (of course, many deviations must be diagnosed). Often the situations will be similar to C undefined-behaviour one (like accesing an array out-of-bounds, signed integer overflow,...). We just say that the code is not standard conforming, that means the standard does not prescribe the outcome of such a code. Such code is not covered but the standard and so anything can result if some compiler (processor) does compile it and you do run it.

这与 处理器相关 行为不同,后者是标准的,只是依赖于实现.

That is different from processor dependent behaviour, that one is standard and just implementation dependent.

只要在 StackOverflow 上进行搜索,就可以为您提供大量示例.像 是否将相同的实体传递给具有不同意图的未定义行为的参数? Fortran 和 MPI_Reduce 如何处理整数溢出?

Just searching here at StackOverflow should give you plenty of examples. Like Is passing the same entity to arguments with different intent undefined behavior? How do Fortran and MPI_Reduce deal with integer overflow?

此答案仅回答所提出的问题,但并未尝试列出 Fortran 中可能发生的所有可能类型的 UB.

This answer just answers the question asked but does not attempt to list all possible kinds of UB that can happen in Fortran.

这篇关于Fortran 是否有未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆