【问题标题】:Working with trees and pointers in Fortran在 Fortran 中使用树和指针
【发布时间】:2015-04-19 20:16:24
【问题描述】:

我一直在开发一个 Fortran 95 程序,它试图猜测您在想什么。它需要一个文件,其行格式为id label question yes no:

9
1 . Is_it_a_living_thing? 2 7
2 . Can_it_walk? 3 6
3 . Does_it_meow? 4 5
4 a_cat . 0 0
5 David_Mitchell . 0 0
6 a_bacteria . 0 0
7 . Is_it_electrical? 8 9
8 a_toaster . 0 0
9 hair_gel . 0 0

下划线的原因是我制作的这个程序的 C 实现,它在阅读时很高兴与 Fortran 的格式配合使用。代码如下:

module types
   implicit none

   type node
      character (len = 32) :: label
      character (len = 128) :: question
      type(node), pointer :: yes, no
   end type node
end module types

program pangolins
   use types
   implicit none

   !type(node), allocatable :: nodes(:)
   type(node), pointer :: head, current

   ! Program

   head => parseFile()
   nullify(current)

   call freeAll(head)

   stop
contains
   function parseFile() result(head)
      implicit none

      type(node), pointer :: nodes(:)
      type(node), pointer :: head
      integer :: i, n, thisN, thisYes, thisNo
      character (len = 32) :: thisLabel
      character (len = 128) :: thisQuestion

      open(10, file = './file1')

      read(10, *) n

      write(*, *) 'Nodes: ', n

      allocate(nodes(n))

      do i = 1, n
         read(10, *) thisN, thisLabel, thisQuestion, thisYes, thisNo

         write (*,'(a24,a64,i4,i4)') thisLabel, thisQuestion, thisYes, thisNo

         nodes(i)%label = thisLabel
         nodes(i)%question = thisQuestion

         if (thisYes .eq. 0) then
            nullify(nodes(i)%yes)
         else
            nodes(i)%yes => nodes(thisYes)
         end if

         if (thisNo .eq. 0) then
            nullify(nodes(i)%no)
         else
            nodes(i)%no => nodes(thisNo)
         end if
      end do

      head => nodes(1)
   end function parseFile

   recursive subroutine freeAll(head)
      implicit none

      type(node), pointer :: head

      if (associated(head%yes)) then
         call freeAll(head%yes)
      end if

      if (associated(head%no)) then
         call freeAll(head%no)
      end if

      write (*,'(a24,a64)') head%label, head%question
      deallocate(head)
   end subroutine freeAll
end program pangolins

当前代码只是初始化数组,然后尝试再次干净地释放它。

该问题与指向数组的指针和数组元素有关。我的函数parseFile 通过首先将文件中的节点解析为指针数组并通过数组中的索引指向是和否指针,然后返回始终是树的头部的第一个元素,从而使树的组织变得容易。这在 C 中很直观,这就是我的出发点。

当我运行此代码时,freeAll() 中对 deallocate() 的第二次调用会导致双重空闲段错误。

我怀疑我将此与使用指针数组的 C 版本混淆了,因此尽管我将起始节点初始化为数组,但我可以使用按顺序遍历作为树一次释放每个指针自初始化以来可能已经变大,然后最终释放数组。这是我一直试图从 C 移植的函数:

...
node_t* readFile(FILE* inFile)
{
    int noOfNodes;

    fscanf(inFile, "%d", &noOfNodes);

    node_t** nodes = (node_t**) malloc(sizeof(node_t*) * noOfNodes);

    for (int i = 0; i < noOfNodes; i++)
        nodes[i] = (node_t*) malloc(sizeof(node_t));

    char* nodeLabel = (char*) malloc(sizeof(char) * MAX_LABEL_SIZE);
    char* nodeQuestion = (char*) malloc(sizeof(char) * MAX_QUESTION_SIZE);
...

我错过了什么?回溯如下:

$ gfortran -pedantic -Wall -ggdb -fbacktrace -fcheck=all -o pangolins pangolins.f95 
pangolins.f95:65.6:

      head => nodes(1)
      1
Warning: Pointer at (1) in pointer assignment might outlive the pointer target
$ ./pangolins
 Nodes:            9
.                       Is_it_a_living_thing?                                              2   7
.                       Can_it_walk?                                                       3   6
.                       Does_it_meow?                                                      4   5
a_cat                   .                                                                  0   0
David_Mitchell          .                                                                  0   0
a_bacteria              .                                                                  0   0
.                       Is_it_electrical?                                                  8   9
a_toaster               .                                                                  0   0
hair_gel                .                                                                  0   0
a_cat                   .                                                               
*** Error in `./pangolins': double free or corruption (out): 0x0000000000858700 ***
======= Backtrace: =========
/lib64/libc.so.6[0x3055875a4f]
/lib64/libc.so.6[0x305587cd78]
./pangolins[0x400d7d]
./pangolins[0x400c70]
./pangolins[0x400c70]
./pangolins[0x400c70]
./pangolins[0x400ddf]
./pangolins[0x4018b6]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x3055821d65]
./pangolins[0x400b69]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:03 5636403                            /home/adam/utils/fortran/pangolins
00602000-00603000 r--p 00002000 08:03 5636403                            /home/adam/utils/fortran/pangolins
00603000-00604000 rw-p 00003000 08:03 5636403                            /home/adam/utils/fortran/pangolins
00853000-00874000 rw-p 00000000 00:00 0                                  [heap]
3055400000-3055420000 r-xp 00000000 08:03 4459030                        /usr/lib64/ld-2.18.so
305561f000-3055620000 r--p 0001f000 08:03 4459030                        /usr/lib64/ld-2.18.so
3055620000-3055621000 rw-p 00020000 08:03 4459030                        /usr/lib64/ld-2.18.so
3055621000-3055622000 rw-p 00000000 00:00 0 
3055800000-30559b4000 r-xp 00000000 08:03 4499543                        /usr/lib64/libc-2.18.so
30559b4000-3055bb3000 ---p 001b4000 08:03 4499543                        /usr/lib64/libc-2.18.so
3055bb3000-3055bb7000 r--p 001b3000 08:03 4499543                        /usr/lib64/libc-2.18.so
3055bb7000-3055bb9000 rw-p 001b7000 08:03 4499543                        /usr/lib64/libc-2.18.so
3055bb9000-3055bbe000 rw-p 00000000 00:00 0 
3056800000-3056905000 r-xp 00000000 08:03 4460722                        /usr/lib64/libm-2.18.so
3056905000-3056b05000 ---p 00105000 08:03 4460722                        /usr/lib64/libm-2.18.so
3056b05000-3056b06000 r--p 00105000 08:03 4460722                        /usr/lib64/libm-2.18.so
3056b06000-3056b07000 rw-p 00106000 08:03 4460722                        /usr/lib64/libm-2.18.so
3057400000-3057415000 r-xp 00000000 08:03 4499572                        /usr/lib64/libgcc_s-4.8.3-20140911.so.1
3057415000-3057614000 ---p 00015000 08:03 4499572                        /usr/lib64/libgcc_s-4.8.3-20140911.so.1
3057614000-3057615000 r--p 00014000 08:03 4499572                        /usr/lib64/libgcc_s-4.8.3-20140911.so.1
3057615000-3057616000 rw-p 00015000 08:03 4499572                        /usr/lib64/libgcc_s-4.8.3-20140911.so.1
7fcb37dc5000-7fcb37dc9000 rw-p 00000000 00:00 0 
7fcb37dc9000-7fcb37e04000 r-xp 00000000 08:03 4471039                    /usr/lib64/libquadmath.so.0.0.0
7fcb37e04000-7fcb38003000 ---p 0003b000 08:03 4471039                    /usr/lib64/libquadmath.so.0.0.0
7fcb38003000-7fcb38004000 r--p 0003a000 08:03 4471039                    /usr/lib64/libquadmath.so.0.0.0
7fcb38004000-7fcb38005000 rw-p 0003b000 08:03 4471039                    /usr/lib64/libquadmath.so.0.0.0
7fcb38005000-7fcb38006000 rw-p 00000000 00:00 0 
7fcb38006000-7fcb38125000 r-xp 00000000 08:03 4470960                    /usr/lib64/libgfortran.so.3.0.0
7fcb38125000-7fcb38325000 ---p 0011f000 08:03 4470960                    /usr/lib64/libgfortran.so.3.0.0
7fcb38325000-7fcb38326000 r--p 0011f000 08:03 4470960                    /usr/lib64/libgfortran.so.3.0.0
7fcb38326000-7fcb38328000 rw-p 00120000 08:03 4470960                    /usr/lib64/libgfortran.so.3.0.0
7fcb3834b000-7fcb3834d000 rw-p 00000000 00:00 0 
7ffdefd3b000-7ffdefd5c000 rw-p 00000000 00:00 0                          [stack]
7ffdefd8e000-7ffdefd90000 r--p 00000000 00:00 0                          [vvar]
7ffdefd90000-7ffdefd92000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

Program received signal SIGABRT: Process abort signal.

Backtrace for this error:
#0  0x7FCB3801F497
#1  0x7FCB3801FADE
#2  0x30558358EF
#3  0x3055835877
#4  0x3055836F67
#5  0x3055875A53
#6  0x305587CD77
#7  0x400D7C in freeall at pangolins.f95:82 (discriminator 2)
#8  0x400C6F in freeall at pangolins.f95:74
#9  0x400C6F in freeall at pangolins.f95:74
#10  0x400C6F in freeall at pangolins.f95:74
#11  0x400DDE in pangolins at pangolins.f95:23
Aborted (core dumped)

第一次解除分配有效,但随后的解除分配会导致问题。请注意write 打印在deallocate() 上方的a_cat 的第二个打印。

【问题讨论】:

  • 抱歉,我错过了这个问题。这不是问题的原因。我将添加一个回溯。

标签: c pointers fortran dynamic-memory-allocation


【解决方案1】:

函数parseFile内的函数结果head与数组的一个元素相关联。虽然元素所在的数组是分配的指针目标,但元素本身不是。

作为函数结果的指针最终作为 freeAll 子例程的参数结束。然后在freeAll 中,您解除分配该指针所引用的事物——也就是说,您正在解除分配不是已分配事物的事物。这是一个编程错误。

如果要在parseFile 函数中释放与nodes 数组关联的指针目标,则需要释放一个数组。也许函数的结果和子程序的参数应该是一个数组。

(在 C 中,指向数组第一个元素的指针可以表示整个数组。在 Fortran 中不是这种情况,除了序列关联之类的东西。)

(风格/安全编程问题 - 您应该考虑使用 parseFile 的子例程而不是函数 - 函数通常用于表达式(不适用于此函数,用于 IO)和它使用返回指针结果的函数很容易出错,所以只应在其他原因需要时使用。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 2018-11-15
    相关资源
    最近更新 更多