分配后字符数组组件的长度错误 [英] Wrong length of a character array component after allocation

查看:33
本文介绍了分配后字符数组组件的长度错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个包含可分配字符数组组件的派生类型.但是,当我尝试在子例程中分配内存时,什么也没有发生.下面的代码示例可能更清楚:

I would like to create a derived type containing allocatable character array components. However, when I try to allocate memory in subroutines, nothing happens. It may be more clear with the code example below:

program test

  type t1
     character(len=:), allocatable :: c(:)
  end type t1

  type(t1) :: t

  call test_string1()
  call test_string2(t)

contains

  subroutine test_string1()
    character(len=:), allocatable :: c(:)

    allocate( character(10) :: c(1) )
    write(*, *) 'Size in string1: ', len(c)
  end subroutine test_string1


  subroutine test_string2(this)
    class(t1) :: this

    allocate( character(10) :: this%c(1) )
    write(*, *) 'Size in string2: ', len(this%c)
  end subroutine test_string2


end program test

我希望此类代码的输出为:

I expect that the output of such a code would be:

 Size in string1:           10
 Size in string2:           10

但是,我实际上得到的是以下内容:

However, what I actually get is the following:

 Size in string1:           10
 Size in string2:            0

因此,第二个子例程没有为 t1%c 分配任何内容.我已经用以下代码编译了代码:

Therefore, the second subroutine allocates nothing for the t1%c... What am I doing wrong here ? I have compiled the code with:

gfortran -c test.f08
gfortran -o test test.o

gfortran 的版本如下:

$ gfortran -v
...
gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)

推荐答案

这是您使用的编译器版本中的错误(或缺乏支持).在gfortran版本7和8中,我看到了相同的问题,但是如上所述,gfortran 9提供了预期的答案.使用其他编译器/版本"是您的问题的答案.

This is a bug (or lack of support) in the version of the compiler you are using. In gfortran versions 7 and 8 I see the same problem, but as noted, gfortran 9 gives the expected answer. "Use a different compiler/version" is an answer to your question.

在这种情况下有时有用的是附加问题:在不更改编译器的情况下,我该怎么办才能解决此错误?"

What is sometimes helpful in cases like this is the additional question: "what can I do to work around this bug without changing compiler?"

该问题的示例非常简单,导致没有太多选择.构造数组的内部分配或源分配都无济于事.

The example of the question is fairly simple leading to not many options. Intrinsic assignment of a constructed array, or sourced allocation don't help.

有有趣的事情吗?为什么是!参数化派生类型.

Is there something interesting that does? Why, yes! Parameterized derived types.

看来gfortran 8遇到了问题,但确实支持派生类型参数化(gfortran 7不支持此Fortran 2003功能).这可能是一个很好的解决方法,或者甚至是解决实际问题的一种很好的替代方法:

It appears that gfortran 8 experiences the problem of the question but does support derived type parameterization (gfortran 7 doesn't support this Fortran 2003 feature). This could be a good work around, or even a good alternative approach to the real problem:

program test

  type t1(length, size)
     integer, len :: length, size
     character(len=length) :: c(size)
  end type t1

  class(t1(:,:)), allocatable :: t

  call test_string1()
  call test_string2(t)

contains

  subroutine test_string1()
    character(len=:), allocatable :: c(:)

    allocate( character(10) :: c(1) )
    write(*, *) 'Size in string1: ', len(c)
  end subroutine test_string1


  subroutine test_string2(this)
    class(t1(:,:)), allocatable :: this

    allocate( t1(10,1) :: this )
    write(*, *) 'Size in string2: ', len(this%c)
  end subroutine test_string2


end program test

这篇关于分配后字符数组组件的长度错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆