问题描述
在C / C ++打印一个指针数组我通常做名@尺寸
。相当于为的Fortran77是什么?
In C/C++ to print a pointer as an array I usually do name@dimension
. What is the equivalent for Fortran77?
推荐答案
Fortran 90的使用描述符来重新present其阵列的尺寸(形状),并通过形状的数组参数。此外,在Fortran指针是特殊的 - 它们只能指向合格的目标。这使得在Fortran中要好得多调试器内省比C / C ++。只需使用打印ARR(指数)
或信息之一
命令 - 不需要花哨的东西。
Fortran 90 uses descriptors to represent the dimensions (the shape) of its arrays and to pass assumed-shape array arguments. Also pointers in Fortran are special - they can only point to qualified targets. This allows much better debugger introspection in Fortran than in C/C++. Just use print arr(index)
or one of the info
commands - no need for fancy stuff.
样code:
program arr
real, dimension(40) :: stack_array
real, allocatable, dimension(:), target :: heap_array
real, dimension(:), pointer :: ptr_array
integer :: i
! Interface required because of the assumed-shape array argument
interface
subroutine foo(bar, baz, qux, ptr)
real, dimension(:) :: bar
real, dimension(40) :: baz
real, dimension(*) :: qux
real, dimension(:), pointer :: ptr
end subroutine foo
end interface
allocate(heap_array(40))
forall(i = 1:40) stack_array(i) = i
heap_array = stack_array + 2
ptr_array => heap_array
print *, stack_array(1)
call foo(stack_array, stack_array, stack_array, ptr_array)
deallocate(heap_array)
end program arr
subroutine foo(bar, baz, qux, ptr)
real, dimension(:) :: bar
real, dimension(40) :: baz
real, dimension(*) :: qux
real, dimension(:), pointer :: ptr
print *, bar(1), baz(1), qux(1), ptr(1)
end subroutine foo
编译调试信息,并与 GDB
运行:
$ gfortran -g -o arr.x arr.f90 && gdb ./arr.x
...
(gdb) info locals
heap_array = (3, 4, 5, 6, 7, 8, 9, 10, 11, 12, ...
ptr_array = (3, 4, 5, 6, 7, 8, 9, 10, 11, 12, ...
stack_array = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...
(gdb) print heap_array
$1 = (3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ...
(gdb) print ptr_array(3:7)
$2 = (5, 6, 7, 8, 9)
...
(gdb) info args
bar = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, ...
baz = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, ...
qux = ()
ptr = (3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ...
它不能显示的假定大小数组参数的原因很明显的内容,但可以单独打印每个元素:
It cannot show the content of assumed-size array arguments for obvious reasons but you can print each element individually:
(gdb) print qux(1)
$5 = 1
(gdb) print qux(2)
$6 = 2
(gdb) print qux(15)
$7 = 15
需要注意的是印刷阵列的部分,因为它们不会被描述传递假定大小数组参数不工作, GDB
遇到麻烦:
(gdb) print qux(1:8)
$8 = (0, 0, 0, 0, 0, 0, 2.25609053e-43, 0)
(gdb) print qux(2:9)
$9 = (0, 0, 0, 0, 0, 0, 2.25609053e-43, 0)
这篇关于如何打印Fortran数组在GDB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!