[英]How to correctly read a C-string into a Fortran string of unspecified length?
The following function is supposed to convert a C string into a Fortran string and works fine in Release builds, but not in Debug:以下 function 应该将 C 字符串转换为 Fortran 字符串,并且在 Release 版本中工作正常,但在 Debug 中不行:
! Helper function to generate a Fortran string from a C char pointer
function get_string(c_pointer) result(f_string)
use, intrinsic :: iso_c_binding
implicit none
type(c_ptr), intent(in) :: c_pointer
character(len=:), allocatable :: f_string
integer(c_size_t) :: l_str
character(len=:), pointer :: f_ptr
interface
function c_strlen(str_ptr) bind ( C, name = "strlen" ) result(len)
use, intrinsic :: iso_c_binding
type(c_ptr), value :: str_ptr
integer(kind=c_size_t) :: len
end function c_strlen
end interface
l_str = c_strlen(c_pointer)
call c_f_pointer(c_pointer, f_ptr)
f_string = f_ptr(1:l_str)
end function get_string
However, it seems that c_f_pointer
does not tell the Fortran pointer-to-string, f_ptr
, the length of the string it is pointing to.但是,似乎c_f_pointer
没有告诉 Fortran 指向字符串的指针f_ptr
它指向的字符串的长度。 In Debug builds, where bounds-checking is active, this results in在调试版本中,边界检查处于活动状态,这会导致
Fortran runtime error: Substring out of bounds: upper bound (35) of 'f_ptr' exceeds string length (0)
I'm using gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
and set the standard to 2008.我正在使用gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
并将标准设置为 2008。
My question: Is there any way to tell the f_ptr
its length without changing the declaration or am I doing something fundamentally the wrong way here?我的问题:有没有办法在不更改声明的情况下告诉f_ptr
它的长度,或者我在这里做的事情从根本上是错误的?
It seems to run correctly if I specify the shape, but for that f_ptr
needs to be an array:如果我指定形状,它似乎可以正确运行,但为此f_ptr
需要是一个数组:
character(len=:), allocatable :: f_string
character(len=1), dimension(:), pointer :: f_ptr
...
call c_f_pointer(c_pointer, f_ptr, [l_str])
However, I cannot find a way to transform that string of rank 1 to the character(len=:), allocatable:: f_string
, which apparently has rank 0.但是,我找不到将等级 1 的字符串转换为character(len=:), allocatable:: f_string
的方法,它显然具有等级 0。
My second question: Is there any way to transfer the f_ptr
data into the f_string
in this example?我的第二个问题:在这个例子中有什么方法可以将f_ptr
数据传输到f_string
中吗?
You cannot use c_f_pointer
to set the length of the Fortran character pointer (F2018, 18.2.3.3):不能使用c_f_pointer
设置Fortran字符指针的长度(F2018,18.2.3.3):
FPTR shall be a pointer, shall not have a deferred type parameter [...] FPTR 应为指针,不应具有延迟类型参数 [...]
A deferred-length character scalar (or array) therefore cannot be used (the length is a type parameter).因此不能使用延迟长度字符标量(或数组)(长度是类型参数)。
You can indeed use a deferred- shape character array as fptr
and then use any number of techniques to copy the elements of that array to the scalar (which is rank-0 as noted).您确实可以使用延迟形状的字符数组作为fptr
,然后使用任意数量的技术将该数组的元素复制到标量(如前所述,它是 rank-0)。
For example, with substring assignment (after explicitly allocating the deferred-length scalar):例如,使用 substring 赋值(在显式分配延迟长度标量之后):
allocate (character(l_str) :: f_string)
do i=1,l_str
f_string(i:i)=fptr(i)
end
Or consider whether you can simply use the character array instead of making a copy to a scalar.或者考虑是否可以简单地使用字符数组而不是复制到标量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.