简体   繁体   English

Fortran:类型未知大小的数组

[英]Fortran: Array of unknown size in type

Perhaps this is a really stupid question and one should really do this differently, but: Is there a possibility to have something like 也许这是一个非常愚蠢的问题,人们应该以不同的方式做到这一点,但是:是否有可能有类似的东西

type food
 INTEGER :: NBananasLeft(NBananaTypes)
 INTEGER :: NApplesLeft(NAppleTypes)
end type food

where NBananaTypes and NAppleTypes is not known at compilation time? 在编译时不知道NBananaTypes和NAppleTypes?

In Fortran 90-95: 在Fortran 90-95:

type food
 INTEGER,pointer :: NBananasLeft(:)
 INTEGER,pointer :: NApplesLeft(:)
end type food

you must allocate the arrays yourself using allocate(var%NBananasLeft(NBananaTypes))) . 你必须使用allocate(var%NBananasLeft(NBananaTypes)))自己分配数组。

In Fortran 2003: 在Fortran 2003中:

type food
 INTEGER,allocatable :: NBananasLeft(:)
 INTEGER,allocatable :: NApplesLeft(:)
end type food

you must also allocate the arrays yourself using allocate(var%NBananasLeft(NBananaTypes))) , but you avoid the possibility of memory leaks. 你还必须使用allocate(var%NBananasLeft(NBananaTypes)))自己分配数组,但是你可以避免内存泄漏的可能性。

In Fortran 2003 by parametrized data types (only a few compilers support that): 在Fortran 2003中,通过参数化数据类型(只有少数编译器支持):

type food(NBananaTypes,NAppleTypes)
 integer,len :: NBananaTypes,NAppleTypes
 INTEGER :: NBananasLeft(NBananaTypes)
 INTEGER :: NApplesLeft(NAppleTypes)
end type food

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM