Silverfrost Forums

Welcome to our forums

Linked list problem: my error or FTN95's?

24 Jul 2007 10:10 #2107

I finally decided to ALLOCATE some time for getting less uncomfortable with the use of POINTERs - specifically to implement a simple linked list facility for CHARACTER data, using code in Metcalf & Reid as a starting point.

The following code contains what seem to me to be two entirely equivalent assignments. FTN95 is quite happy with code that uses the line commented out with 😃. However, using instead the two lines commented out with 😦, the CHECKMATE build gives the runtime error message 'Reference through unset FORTRAN pointer', and DEBUG and RELEASE builds report access violation.

I know the use of POINTER is fraught with subtle traps for the uninitiated - that's why I've avoided using them for as long as I have - so I'm not confident my code doesn't contain one such, but it looks like a compiler bug to me :? . If it's my error, could someone please explain the distinction between the two ways of doing the assignment?

Andy

  program stack_tester
  use subs
  use stack_tracker
  call create_stack
  call a
  stop
  end program stack_tester

  module subs
  use stack_tracker
  contains

!------------- subroutine a call push_name ('a') call pop_name return end subroutine a !------------- end module subs

  module stack_tracker
  integer, parameter :: worlen = 16
  type stack_id
    character (len = worlen) has_control
    type (stack_id), pointer :: had_control        
  end type stack_id
  type (stack_id), pointer :: stack_head, stack_item
  contains

!------------- subroutine create_stack allocate (stack_head) stack_head% has_control = 'Main' nullify (stack_head% had_control) return end subroutine create_stack !------------- subroutine push_name (routine_name) character (len = *) routine_name allocate (stack_item) !:-) stack_item = stack_id (routine_name, stack_head) !:-( stack_item% has_control = routine_name !:-( stack_item% had_control = stack_head stack_head ⇒ stack_item return end subroutine push_name !------------- subroutine pop_name stack_item ⇒ stack_head stack_head ⇒ stack_head% had_control deallocate (stack_item) return end subroutine pop_name !------------- end module stack_tracker

3 Oct 2007 9:23 #2290

This has sat a long time with no reply and has had a lot of views, so maybe other people are puzzled too. Or maybe no-one else wants to risk looking daft 😉 I actually got a reply from Silverfrost on the subject quite some time ago, and I thought I'd share it with the forum now that I've started a new topic associated with linked lists. So, for the record, this is what Robert had to say:

*I think the problem is misinterpretation of this line:

stack_item% had_control = stack_head

It looks like you are assigning the pointer had_control to be stack_head – and in just about every computer language that would be the outcome. However, in Fortran, this line means 'the thing that stack_item%had_control' points to, assign it stack_head. Stack_item has just been allocated and so the pointer had_control is null and you get an access violation.

It fooled me too until I looked at the assembly output and saw the copy (press F11 in SDBG).*

I still had to think hard about this answer but I got it in the end. I take solace in the fact that Robert had to look at assembler code to grok the problem; that is not an option that would have helped me ...

Andy

Please login to reply.