简体   繁体   English

运行包含'nullify'的FORTRAN代码时出现Linux错误

[英]Linux error when running FORTRAN code which includes 'nullify'

I started learning FORTRAN a few days ago, and I've stumbled across an error when trying to 'nullify' a pointer. 几天前,我开始学习FORTRAN,在尝试“无效化”指针时偶然发现了一个错误。 The code is compiled without any flaw, but I this is what it displays after inputing the variable. 代码被编译时没有任何缺陷,但是这是输入变量后显示的内容。 I also know the problem is caused by the 'nullify' command, because the program runs without any error if I comment the line. 我也知道问题是由'nullify'命令引起的,因为如果我注释该行,则程序运行时不会出现任何错误。

Code: 码:

program demo1
   implicit none
   TYPE :: Neuron
      real :: val = 0
      real :: w1 = 1, w2 = 1
      TYPE(Neuron), pointer :: vertex1, vertex2
   end TYPE Neuron

   TYPE :: container
      TYPE(Neuron), pointer :: obj
   end TYPE container

   integer :: i=0, n_inputs
   TYPE(Neuron), target :: output_vertex
   TYPE(Neuron), pointer :: temp 
   TYPE(container), allocatable, dimension(:) :: inputs

   read (*,*) n_inputs 

   allocate(inputs(0:n_inputs))

   inputs(0)%obj=>output_vertex

  !do i=1, n_inputs
      temp=>inputs(i-1)%obj

      nullify(inputs(i-1)%obj)
      !allocate(inputs(i)%obj)
      !allocate(inputs(i-1)%obj)

      !temp%vertex1=>inputs(i)%obj
      !temp%vertex2=>inputs(i-1)%obj
   !end do

end program demo1

Error outputted: 输出错误:

    *** glibc detected *** ./demo1.o: free(): invalid pointer: 0x08dafc18 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb74b8ee2]
./demo1.o[0x8048855]
./demo1.o[0x8048899]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb745c4d3]
./demo1.o[0x80485e1]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:02 2621510    /media/work/teme/neural_network/demo1.o
08049000-0804a000 r--p 00000000 08:02 2621510    /media/work/teme/neural_network/demo1.o
0804a000-0804b000 rw-p 00001000 08:02 2621510    /media/work/teme/neural_network/demo1.o
08dac000-08dcd000 rw-p 00000000 00:00 0          [heap]
b7370000-b738c000 r-xp 00000000 08:01 4195265    /lib/i386-linux-gnu/libgcc_s.so.1
b738c000-b738d000 r--p 0001b000 08:01 4195265    /lib/i386-linux-gnu/libgcc_s.so.1
b738d000-b738e000 rw-p 0001c000 08:01 4195265    /lib/i386-linux-gnu/libgcc_s.so.1
b73a1000-b73a2000 rw-p 00000000 00:00 0 
b73a2000-b73cc000 r-xp 00000000 08:01 4199147    /lib/i386-linux-gnu/libm-2.15.so
b73cc000-b73cd000 r--p 00029000 08:01 4199147    /lib/i386-linux-gnu/libm-2.15.so
b73cd000-b73ce000 rw-p 0002a000 08:01 4199147    /lib/i386-linux-gnu/libm-2.15.so
b73ce000-b7440000 r-xp 00000000 08:01 3150891    /usr/lib/i386-linux-gnu/libquadmath.so.0.0.0
b7440000-b7441000 r--p 00071000 08:01 3150891    /usr/lib/i386-linux-gnu/libquadmath.so.0.0.0
b7441000-b7442000 rw-p 00072000 08:01 3150891    /usr/lib/i386-linux-gnu/libquadmath.so.0.0.0
b7442000-b7443000 rw-p 00000000 00:00 0 
b7443000-b75e7000 r-xp 00000000 08:01 4199152    /lib/i386-linux-gnu/libc-2.15.so
b75e7000-b75e9000 r--p 001a4000 08:01 4199152    /lib/i386-linux-gnu/libc-2.15.so
b75e9000-b75ea000 rw-p 001a6000 08:01 4199152    /lib/i386-linux-gnu/libc-2.15.so
b75ea000-b75ed000 rw-p 00000000 00:00 0 
b75ed000-b76eb000 r-xp 00000000 08:01 3154416    /usr/lib/i386-linux-gnu/libgfortran.so.3.0.0
b76eb000-b76ec000 r--p 000fe000 08:01 3154416    /usr/lib/i386-linux-gnu/libgfortran.so.3.0.0
b76ec000-b76ed000 rw-p 000ff000 08:01 3154416    /usr/lib/i386-linux-gnu/libgfortran.so.3.0.0
b76ed000-b76ee000 rw-p 00000000 00:00 0 
b7700000-b7703000 rw-p 00000000 00:00 0 
b7703000-b7704000 r-xp 00000000 00:00 0          [vdso]
b7704000-b7724000 r-xp 00000000 08:01 4199142    /lib/i386-linux-gnu/ld-2.15.so
b7724000-b7725000 r--p 0001f000 08:01 4199142    /lib/i386-linux-gnu/ld-2.15.so
b7725000-b7726000 rw-p 00020000 08:01 4199142    /lib/i386-linux-gnu/ld-2.15.so
bfb5a000-bfb7b000 rw-p 00000000 00:00 0          [stack]
Aborted (core dumped)

Help would be appreciated. 帮助将不胜感激。

The problem is, that your want to nullify a non-existing pointer. 问题是,您想使不存在的指针无效。

You want to nullify the pointer inputs(-1)%obj which obviously doesn't exist, because you didn't allocate the (-1)st element of inputs. 您想使指针inputs(-1)%obj无效,因为它没有分配输入的第(-1)st元素,因此显然不存在该指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM