想在fortran中定义一个动态大小的数组,数组的大小从文件中读入,所以开始不知道。 我知道在C++中可以 int *p2i = new int[size]; //.... 在fortran中怎么做?谢谢
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中怎么做?谢谢
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.
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.