此FORTRAN代码不应编译.有什么理由吗? [英] This FORTRAN code shouldn't compile. Is there a reason why it does?

查看:53
本文介绍了此FORTRAN代码不应编译.有什么理由吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码可以编译,但我认为不应该.如您所见,输出是垃圾.

The following code compiles, but I do not think that it should. As you can see, the output is garbage.

这是一个最小的失败示例,在我正在从事的一个大型项目中,这使我很难受.

This is a minimal failing example of something that bit me hard in a large project I work on.

我的问题是-为什么编译器不抱怨?这是编译器的限制,还是某种预期的行为",而我错过了一些东西?

My question is - why does the compiler not complain? Is this a compiler limitation, or is this somehow "expected behaviour", and I've missed something?

我正在使用gfortran 4.6.3.

I'm using gfortran 4.6.3.

module dataModule
    integer :: datum1 = int(1)
    integer :: datum2 = int(2)    
end module dataModule

program moduleTest
    use dataModule, only: datum1

    write(*,*) "datum 1 is", datum1
    write(*,*) "datum 2 is", datum2

end program moduleTest

示例输出:

datum 1 is           1
datum 2 is  4.58322689E-41

推荐答案

您的代码出错了,而不是编译器出错了.尽管 only 子句和 if datum2 If datum2 关联>被忽略,然后是的,那将是一个顽皮的编译器.

Your code is at fault, not the compiler. If datum2 were use associated despite the only clause and if the explicit initialization of datum2 were ignored, then yes, that would be a naughty compiler.

不过,答案要平凡得多.

The answer is much more mundane, though.

datum2 不使用关联:在没有隐式无的情况下,它是主程序中的隐式类型变量.垃圾"源于以下事实:在引用其值之前未通过初始化或赋值对其进行定义,并且它是隐式(默认)实数.不需要编译器在编译(或运行)时检测到此错误.

datum2 is not use associated: in the absence of implicit none it is an implicitly typed variable in the main program. The "garbage" comes from the fact that it is not defined, by initialization or assignment, before its value is referenced and that it's implicitly (default) real. The compiler isn't required to detect this mistake at compile (or run) time.

这篇关于此FORTRAN代码不应编译.有什么理由吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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