Fortran 2003 中未初始化的变量
与 f90 相比,为什么 fortran 2003 中的变量没有初始化为零?
我在文件的函数中有一个变量。 它初始化为 0。我想使用它的另一个函数,然后它显示一个垃圾值。 即使对于全局变量也是如此。 我需要为 fortran 2003 编译器设置任何选项吗?
Why the variables are not initializing to zero in fortran 2003 when compared with f90?
I have a variable in a function from a file. its initialized to 0. I want to use it another function then it shows a garbage value. even for global variables also. Is there any option I need to set for fortran 2003 compiler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试使用 -zero 或 /Qzero - 这些会将本地标量初始化为零 - 但您确实应该显式设置初始值。 正如您所发现的,依靠编译器来为您完成此操作是引入错误的好方法。 请注意,不同编译器的选项名称可能不同。 提到的内容适用于 英特尔 Visual Fortran。
You could try using -zero or /Qzero -- these will initialize local scalars to zero -- but you really should be explicitly setting initial values. Depending on the compiler to do it for you is, as you have found out, a good way to introduce bugs. Note that the option names may be different for different compilers. The ones mentioned are for Intel Visual Fortran.
我们经历了从 Compaq Visual Fortran 到 Intel Visual Fortran 的转变。 尽管他对 Fortran 编译器缺乏熟悉,但 Workshop Alex 留下的整篇文章都是正确的——你不应该依赖编译器设置初始值。 标准并未规定应自动设置变量值。 即使确实如此,依赖这种编译器行为也是有风险的。
Compaq Visual Fortran 自动初始化变量。 其他编译器则不然。 您的代码需要修复。 您只能通过初始化所有变量来做到这一点。
约翰
We experienced this moving from Compaq Visual Fortran to Intel Visual Fortran. Notwithstanding his lack of familiarity with Fortran compilers, the entire post left by Workshop Alex is correct--you shouldn't rely on the compiler setting initial values. The Standard doesn't say variable values should automatically be set. Even if it did, relying on this compiler behavior is risky.
Compaq Visual Fortran automatically initializes variables. Other compilers do not. Your code needs to be fixed. You can only do that by initializing all your variables.
John
我不熟悉任何 Fortran 编译器,但我确实知道,一般来说,大多数编译器不会初始化全局和局部变量。 初始化应始终在代码中完成。 您不应该依赖编译器来为您执行此操作。
您看到的垃圾可能来自堆栈或内存堆。 有些编译器在分配内存时会用零填充堆,这可以解释为什么有些编译器似乎用 0 初始化变量。它们实际上没有初始化任何东西,它们只是使用了碰巧用零填充的内存区域。 。
I'm unfamiliar with any Fortran compiler but I do know that in general, most compilers won't initialize global and local variables. Initialization should always be done in code. You should not rely on the compiler to do this for you.
The garbage you're seeing is probably from the stack or memory heap. Some compilers will fill the heap with zero's when allocating memory which could explain why some compilers will seem to initialize variables with 0. They haven't actually initialized anything, they're just using a memory area that happened to be filled with zero's...
Fortran 90 和 Fortran 2003 在变量初始化方面没有区别。 所有有效的 Fortran 90 代码都是有效的 Fortran 2003,并且应该给出相同的结果(除了极少数模糊的极端情况,其中依赖于编译器的行为现在由标准指定;这不是其中之一)。
现在,至于为什么你会看到差异,在不知道你的编译器是什么以及你的代码到底做什么的情况下很难说。 我强烈怀疑您依赖于编译器相关的行为,并且当您更改编译器时它会崩溃。
There is no difference between Fortran 90 and Fortran 2003 in initialisation of variables. All valid Fortran 90 code is valid Fortran 2003, and should give the same result (except for very few obscure corner-cases where what was compiler-dependant behaviour is now specified by the standard; this is not one of those).
Now, as to why you could see a difference, it's hard to say without knowing what your compilers are, and what your code does exactly. I strongly suspect you were relying on compiler-dependant behaviour, and it broke when you changed compiler.