假设大小的数组:冒号与星号 - DIMENSION(:) arr 与 arr(*) [英] Assumed size arrays: Colon vs. asterisk - DIMENSION(:) arr vs. arr(*)

查看:23
本文介绍了假设大小的数组:冒号与星号 - DIMENSION(:) arr 与 arr(*)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两种声明假定大小数组的方法有什么区别吗?

Is there any difference between these two methods of declaring an assumed-size array?

例如

real, dimension(:) :: arr

real               :: arr(*)

推荐答案

表格

real, dimension(:) :: arr

声明一个假定形状数组,而形式

real :: arr(*)

声明一个 假定大小 数组.

而且,是的,它们的用法存在差异.之所以出现差异,是因为编译器大约知道"假定形状数组的形状,但不知道假定大小数组的形状.编译器可用的额外信息意味着,除其他外,假定形状数组可用于全数组表达式.假定大小的数组仅当它是不需要数组形状的过程引用中的实际参数时才能在整个数组表达式中使用.哦,还有对内在 lbound 的调用——但不是在对内在 ubound 的调用中.仔细阅读标准或一本优秀的 Fortran 书籍,您会发现其他一些细微但不那么细微的差异.

And, yes, there are differences between their use. The differences arise because, approximately, the compiler 'knows' the shape of the assumed-shape array but not of the assumed-size array. The extra information available to the compiler means that, among other things, assumed-shape arrays can be used in whole-array expressions. An assumed-size array can only be used in whole array expressions when it's an actual argument in a procedure reference that does not require the array's shape. Oh, and also in a call to the intrinsic lbound -- but not in a call to the intrinsic ubound. There are other subtle, and not-so-subtle, differences which your close reading of the standard or of a good Fortran book will reveal.

给 Fortran 新手的一些建议是尽可能使用假定形状的数组.它们在 Fortran 90 之前不可用,因此您会在旧代码中看到许多假定大小的数组.假设形状数组在新代码中更好,因为 shapesize 函数可用于查询它们的大小以避免越界错误和分配维数取决于输入数组维数的数组.

Some advice for new Fortran programmers is to use assumed-shape arrays when possible. They were not available before Fortran 90, so you will see lots of assumed-size arrays in old code. Assumed-shape arrays are better in new code, because the shape and size functions can be used to query their sizes to avoid out-of-bounds error and to allocate arrays whose dimensions depend on the dimensions of input arrays.

这篇关于假设大小的数组:冒号与星号 - DIMENSION(:) arr 与 arr(*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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