forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Linked list problem: my error or FTN95's?

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
sparge



Joined: 11 Apr 2005
Posts: 371

PostPosted: Tue Jul 24, 2007 11:10 am    Post subject: Linked list problem: my error or FTN95's? Reply with quote

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 Smile. However, using instead the two lines commented out with Sad, 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 Confused . 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)
!Smile stack_item = stack_id (routine_name, stack_head)
!Sad stack_item% has_control = routine_name
!Sad 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
Back to top
View user's profile Send private message Send e-mail
sparge



Joined: 11 Apr 2005
Posts: 371

PostPosted: Wed Oct 03, 2007 10:23 am    Post subject: Reply with quote

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 Wink 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
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group