解除分配后数组的大小 [英] Size of array after a deallocate

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

问题描述

我创建了一个可分配的数组.我分配元素,然后打印数组的大小.我觉得奇怪的是,释放后大小保持不变.

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.

size 的参数不能是未分配的可分配变量.

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

对于您的代码,fred 是一个可分配的变量.如果这个代码块被执行,那么在最后一行 size 有一个参数,它是一个未分配的可分配变量.在这种情况下,如果它构成程序(程序单元)的一部分,那么该程序(程序单元)就不是符合标准的程序(程序单元).

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).

这种不一致性并不是 Fortran 处理器需要检测为符合 Fortran 处理器的不一致性.

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

是的,处理器检测到这一点会很好,如果您在编译时选择适当的选项,很多都会检测到.按照流行的说法,一个符合标准的处理器将被允许启动第三次世界大战以响应此代码.它也可以打印3.这完全取决于编译器供应商的心血来潮.

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]

fred 未被分配时无效,但当它被分配但大小为零时;并且在后一种情况下不需要特殊处理.

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.

我同意 High Performance Mark 的评论,如果编译器要返回任何值,0 是一个糟糕的选择.由于大小没有很好地定义,任何访问 fred(3) 的尝试,比如说,基于返回的大小,也是一个坏主意.同样,编译器可以自由地为此引用提供任何特定值.

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.

最后,如果您想检查数组是否已分配,您应该使用 allocated 内部函数,而不是依赖 size 返回 0.当然,在这种情况下,它是不需要的,因为您可以确定在 deallocate 语句之后 fred 确实没有分配.

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.

在 Fortran 90 中,分配状态可能是未定义的,然后甚至 allocated 都是不允许的.

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

同样的想法也适用于数组和 size 内在以外的东西.size 是一个查询函数,它要求查询的数组被分配(如果是可分配的实体)或关联的指针(如果是指针).同样,例如,必须分配具有延迟长度的字符/指针关联(如适用)作为 len 内部查询函数的参数.

The same ideas apply for things other than arrays and the size intrinsic. size is an inquiry function which requires the array inquired about to be allocated (if an allocatable entity) or pointer associated (if a pointer). Equally, for example, a character with deferred length must be allocated/pointer associated (as applicable) to be an argument to the len intrinsic inquiry function.

查询函数并不总是需要分配/关联参数.例如,即使未分配变量,也可能会询问显式长度的字符的长度.应该检查文档中的要求,不要进行不允许的查询.

Inquiry functions don't always require arguments to be allocated/associated. For example, the length of a character of explicit length may be asked about even if the variable is not allocated. One should check the requirements in documentation and not make inquiries which are not allowed.

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

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