Fortran 90 Differences in declaring allocatable array

喜欢而已 提交于 2019-12-10 14:47:44

问题


Is there a difference between

integer, intent(in) :: n
integer, dimension(:), allocatable :: a
allocate(a(n))

and

integer, intent(in) :: n
integer, dimension(n) :: a

In which situation would we use the first version? Perhaps I misunderstood allocatable array, is the second version even an allocatable array?


回答1:


The second case indeed doesn't have a allocatable. It is, however, an automatic object.

Ignoring the practical differences in memory use at the implementation level, there is a big difference. Yes, each a is (assuming things not explicitly stated in the question) a local variable which is, after the allocate and the automatic creation, of size n, but in the first case it is allocatable. It can be deallocated, reallocated (perhaps to a different size), and deallocated again. And so on.

The automatic object (second case) cannot be.




回答2:


The first case is an allocatable array. The number of elements in the array may be dynamically allocated or reallocated at runtime at any scope.

The second case is an automatic array of a fixed number of elements defined by the dummy argument. Its size can only be changed locally within the procedure it is called in, according to the size passed in the dummy argument.



来源:https://stackoverflow.com/questions/24337658/fortran-90-differences-in-declaring-allocatable-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!