Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Fortran 是否需要分配一个可分配数组?_Fortran_Allocation - Fatal编程技术网

Fortran 是否需要分配一个可分配数组?

Fortran 是否需要分配一个可分配数组?,fortran,allocation,Fortran,Allocation,我有一个关于派生类型和分配数组的简单问题。 假设我们已经定义了类型 type demo_type real(kind=8),allocatable :: a(:) end type demo_type 以及功能 function demo_fill(n) result(b) integer, intent(in) :: n real(kind=8) :: b(n) b = 1.d0 end function demo_fill

我有一个关于派生类型和分配数组的简单问题。 假设我们已经定义了类型

type demo_type
        real(kind=8),allocatable :: a(:)
end type demo_type
以及功能

function demo_fill(n) result(b)
        integer, intent(in) :: n
        real(kind=8) :: b(n)
        b = 1.d0
end function demo_fill
在主程序中写入是否正确

type(demo_type) :: DT
DT%a = demo_fill(3)
还是有必要先分配DT%a以防止意外覆盖内存中的其他变量

type(demo_type) :: DT
allocate(DT%a(3))
DT%a = demo_fill(3)

它们都是编译的,但我想知道哪种方法是正确的。谢谢

在Fortran 2003中,任何可分配数组都可以在分配时自动分配

就你而言

DT%a = demo_fill(3)
使
DT%a
自动分配到右侧的形状,这是该功能的结果,如果以前未分配到该形状

这种行为是标准的,但某些编译器在默认情况下不启用它,特别是英特尔Fortran。必须使用特殊的编译器标志来启用它(
-标准语义
-假设realloc_lhs


作为旁注,
kind=8
并不意味着所有编译器上都有8字节的实数,这种用法是不受欢迎的(请参阅)。

在Fortran 2003中,任何可分配数组都可以在分配时自动分配

就你而言

DT%a = demo_fill(3)
使
DT%a
自动分配到右侧的形状,这是该功能的结果,如果以前未分配到该形状

这种行为是标准的,但某些编译器在默认情况下不启用它,特别是英特尔Fortran。必须使用特殊的编译器标志来启用它(
-标准语义
-假设realloc_lhs


作为补充说明,
kind=8
并不意味着所有编译器上都有8字节real,这种用法是不受欢迎的(请参阅)。

感谢您的快速回答!谢谢你的快速回答!