Redian新闻
>
如何在fortran中定义一个动态的数组?
avatar
如何在fortran中定义一个动态的数组?# Programming - 葵花宝典
f*w
1
想在fortran中定义一个动态大小的数组,数组的大小从文件中读入,所以开始不知道。
我知道在C++中可以
int *p2i = new int[size];
//....
在fortran中怎么做?谢谢
avatar
O*e
2
Dynamic allocation in heap memory using Fortran 90/95:
integer,dimension(:),allocatable::p2i
integer::size
integer::alloc_stat
read(some_file,some_format)size
allocate(p2i(1:size),stat=alloc_stat)
if(alloc_stat.ne.0)stop"error allocating p2i"
...
deallocate(p2i,stat=alloc_stat)
if(alloc_stat.ne.0)stop"error deallocating p2i"
If you want to call a subroutine to allocate p2i, return from that
subroutine, use p2i elsewhere, then call another subroutine to deallocate
p2i, you will need to declare p2

【在 f**********w 的大作中提到】
: 想在fortran中定义一个动态大小的数组,数组的大小从文件中读入,所以开始不知道。
: 我知道在C++中可以
: int *p2i = new int[size];
: //....
: 在fortran中怎么做?谢谢

avatar
f*w
3
Thank you. Although I still have a question,
what is the meaning of :: here?
I know it is scope operator in C++, does it have a similar meaning here?
Thanks.
avatar
O*e
4
The :: is just the new syntax for declarations in Fortran 90/95/2003.
In F77 you would write
INTEGER X
DIMENSION X(1:N)
or more compactly
INTGETER X(1:N)
and in F90+ you would write
integer,dimension(1:n)::x
The :: separates the type and properties from the list of variables.
It does not have the same meaning as :: in C++.
You can probably even omit the :: (at least in some cases you can).
I have gotten into the habit of always writing the :: where possible.

【在 f**********w 的大作中提到】
: Thank you. Although I still have a question,
: what is the meaning of :: here?
: I know it is scope operator in C++, does it have a similar meaning here?
: Thanks.

avatar
f*w
5
thanks
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。