保留固有分配中分配的界限 [英] Preserve bounds in allocation in intrinsic assignment

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

问题描述

我在分配时使用自动分配来计算两个数组的差,其边界从0开始:

I am using automatic allocation on assignment to calculate the difference of two arrays, with bounds starting at 0:

program main
   implicit none
   integer, allocatable :: a(:), b(:), c(:)

   allocate(a(0:10))
   allocate(b(0:10))

   a = 1
   b = 2

   write (*,*) lbound(a)
   write (*,*) lbound(b)

   c = b - a

   write (*,*) lbound(c)
end program main

gfortran和ifort都给出输出:

Both, gfortran and ifort give the output:

       0
       0
       1

为什么c的边界与a和b的边界相同?是否有一种简短而简洁的方法(无需显式分配)来确保c具有相同的边界?

Why doesn't c have the same bounds as a and b? Is there a short and concise (without an explicit allocate) way of making sure c has the same bounds?

推荐答案

为什么c与und b没有相同的界限?

因为它将分配给与 a b 相同的形状,并且从1开始.(无论如何, ba 是一个表达式,也从1开始,但并不重要.如果 b a 从不同的索引处开始怎么办?)

Because it shall be allocated to the same shape as a and b and starting at 1. (Anyway b-a is an expression and it starts at 1 as well, but it is not important. What if b and a start at different indexes?)

是否有一种简短而简洁的方法(无需显式分配)来确保c具有相同的边界?

不.但是您至少可以使用 mold = a source = a 显式分配.

No. But you can at least explicitly allocate with mold=a or source=a.

这篇关于保留固有分配中分配的界限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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