阵列的取消分配后的大小 [英] Size of array after a deallocate

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

问题描述

我已经创建了一个分配数组。我分配元素,然后我打印数组的大小。我觉得奇怪的是,大小仍然是一个解除分配后的相同。

 整数,可分配::弗雷德(:)
  分配(弗雷德(3))
  写(*,*)弗雷德,尺寸(弗雷德)
  DEALLOCATE(弗雷德)
  写(*,*)弗雷德,尺寸(弗雷德)


解决方案

这是迫切需要一个规范的一个问题,真的。在回答您的具体问题在没有利益(据我所知道的,但是我可以写一个最终),我会回答。

尺寸参数不能是未分配的可分配变量。

有关您的code 弗雷德可分配的变量。如果执行code的这个块,然后在最后一行尺寸有一个参数是未分配的可分配变量。在如果它形成了一个节目(节目单元)的一部分,那么该程序(程序单元)这种情况不是符合标准的程序(程序单元)。

这缺乏一致性不是一个Fortran处理器需要检测作为合格的Fortran处理器缺乏一致性。

是的,这将是很好的处理器检测到这一点,很多人会,如果你选择在编译的时候适当的选项。一个符合标准的处理器将被允许,在流行的说法,开始第三次世界大战针对这一code。它也可以打印 3 。这是完全下降到编译器供应商的兴致。

在注释提示,了解更多信息。

这是诱人的,或许,期待一个释放的数组的大小是零。但是,释放的阵列和零元素的数组是完全不同的东西,就像零长度的字符不一样的未分配的可分配角色。

在特定的,我们有类似的成语

 弗雷德= [弗雷德,追加]

这是不是当弗雷德未分配有效的,但是当它被分配但大小为0的有;它并不需要在这后一种情况下特殊的处理。

我同意高性能Mark的评论说,如果编译器是要返回任何值, 0 是一个不错的选择。多为大小没有明确定义任何试图访问弗雷德(3),也就是说,根据返回的大小,也是一个坏主意。同样,编译器是免费给这个参考任何特定的值。

最后,如果你想检查一个数组是否被分配你应该使用分配内在而不是依赖于尺寸返回 0 。当然,在这种情况下,没有必要因为你可以相当肯定的取消分配语句之后弗雷德是确实不分配。

Fortran 90中是可能的分配状态是不确定的,那么即使分配是不允许的。

I have created an allocatable array. I allocate the elements and then I print the size of the array. I find strange that the size remains the same after a deallocate.

  Integer, Allocatable :: fred(:)
  Allocate (fred(3))
  Write (*,*) "fred: ", Size (fred)
  Deallocate (fred)
  Write (*,*) "fred: ", Size (fred)

解决方案

This is a question crying out for a canonical one, really. In the interest of answering your specific question in the absence (as far as I can tell, but I may write one eventually), I'll answer.

The argument to size must not be an unallocated allocatable variable.

For your code fred is an allocatable variable. If this block of code is executed then on the final line size has an argument which is an unallocated allocatable variable. In this case if it forms part of a program (program unit) then that program (program unit) is not a standard conforming program (program unit).

This lack of conformance is not a lack of conformance that a Fortran processor is required to detect to be a conforming Fortran processor.

Yes, it would be nice for the processor to detect this, and many will if you choose the appropriate options at compile time. A standard conforming processor will be allowed, in the popular parlance, to start World War III in response to this code. It may also print 3. That is entirely down to the whim of the compiler vendor.

Prompted by the comments, more information.

It's tempting, perhaps, to expect that the size of a deallocated array is zero. However, a deallocated array and an array with zero elements are quite different things, much as a character of length zero is not the same as an unallocated allocatable character.

In particular, we have idioms like

fred = [fred, append]

which are not valid when fred is not allocated, but are when it is allocated but of size zero; and it doesn't need special treatment in this latter case.

I agree with High Performance Mark's comment that, if the compiler is going to return any value, 0 is a bad choice. Much as the size isn't well defined any attempt to access fred(3), say, based on the returned size, is also a bad idea. Again, the compiler is free to give any particular value for this reference.

Finally, if you want to check whether an array is allocated you should use the allocated intrinsic rather than rely on size returning 0. Of course, in this case it isn't needed as you can be quite sure that after the deallocate statement fred is indeed not allocated.

In Fortran 90 it was possible for allocation status to be undefined and then even allocated wasn't allowed.

这篇关于阵列的取消分配后的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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