Fortran 未分配数组 [英] Fortran unassigned array

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

问题描述

我刚开始学习 Fortran,遇到了这个问题.考虑下面的简单代码.

I just begin learning Fortran, and I come across this issue. Consider the following simple code.

PROGRAM random

INTEGER, DIMENSION(12):: array
PRINT *, array

END PROGRAM random

array没有赋值,但是可以打印,而且好像有几个随机元素和几个零元素.但是,如果我考虑更短的数组,请说我声明

The array is not assigned value, but can be printed, and it seems to have a few random elements and several zero elements. However, if I consider a shorter array, say I declare

INTEGER, DIMENSION(5):: array

那么打印的数组的所有元素都为 0.我想知道这里发生了什么?

then the printed array has all elements = 0. I wonder what is happening here?

推荐答案

当您定义一个数组并尝试在初始化它之前查看它包含的值(即通过打印)时,行为是未定义的.这取决于编译器到编译器.虽然一个编译器可能会自动将所有值设置为零(我们中的许多人认为这是默认值),但另一个编译器可能会将其设置为完全随机的值.这就是为什么您会看到数组值有时为零有时不是.

When you define an array and try to look at the values it contains (i.e. by printing) before initialising it, the behaviour is undefined. It depends on compiler to compiler. While one compiler may automatically set all the values to zero (which many of us think that to be the default), another compiler may set it to completely random values. Which is why you are seeing the array values sometimes zero and sometimes not.

但是,许多编译器都可以选择在编译器级别将未分配的数组初始化为零.始终建议在使用数组之前始终对其进行初始化!

However, many of the compilers have options to initialise an unassigned array to zeros at compiler level. It is always advised that one should initialise an array always before using it!

这篇关于Fortran 未分配数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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