2

I started learning FORTRAN a few days ago, and I've stumbled across an error when trying to 'nullify' a pointer. 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.

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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Such a novice error! I was fooled by the commented 'do i=1,n_inputs' line. Thank you. :]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.