C 在Fortran中使用树和指针

C 在Fortran中使用树和指针,c,pointers,fortran,dynamic-memory-allocation,C,Pointers,Fortran,Dynamic Memory Allocation,我一直在做一个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 ha

我一直在做一个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
通过首先将文件中的节点解析为指针数组,并通过数组中的索引指向yes和no指针,然后返回第一个元素(始终是树的头),从而简化了树的组织。这在C语言中是很直观的,这就是我的来源

当我运行此代码时,在
freeAll()
中对
deallocate()
的第二次调用会导致双自由segfault

我怀疑我把这与使用指针数组的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);
...

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

上方打印,函数
解析文件中的函数结果
与数组的一个元素相关联。虽然元素所属的数组是分配的指针目标,但元素本身不是

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

如果要在
parseFile
函数中取消分配与
节点
数组关联的指针目标,则需要取消分配数组。也许函数的结果和子例程的参数应该是一个数组

(在C语言中,指向数组第一个元素的指针可以表示整个数组。在Fortran语言中,除了序列关联之类的东西外,情况并非如此。)


(风格/安全编程问题)-您应该考虑使用“代码> PARSEFILE < /代码>的子程序,而不是函数-函数通常用于表达式(不适用于此函数,即IO)。而且返回指针结果的函数很容易出错,因此只有在出于其他原因需要时才应该使用它们。)

抱歉,我没有回答这个问题。这不是问题的原因。我将添加一个回溯跟踪。
$ 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)